Skip to main content
Back to problems
#2011
Easy Algorithms

Final value of variable after performing operations

Array String Simulation
90.6% acceptance
Feb 25, 2026
2020
216
There is a programming language with only four operations and one variable X: ++X and X++ increments the value of the variable X by 1. --X and X-- decrements the value of the variable X by 1. Initially, the value of X is 0. Given an array of strings operations containing a list of operations, return the final value of X after performing all the operations.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn final_value_after_operations(operations: Vec<String>) -> i32 {
    operations.iter().map(|op| if op.contains('+') { 1 } else { -1 }).sum()
  }
}