Problem

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 _thefinal value of _X after performing all the operations.

Examples

Example 1

1
2
3
4
5
6
7
Input: operations = ["--X","X++","X++"]
Output: 1
Explanation:  The operations are performed as follows:
Initially, X = 0.
--X: X is decremented by 1, X =  0 - 1 = -1.
X++: X is incremented by 1, X = -1 + 1 =  0.
X++: X is incremented by 1, X =  0 + 1 =  1.

Example 2

1
2
3
4
5
6
7
Input: operations = ["++X","++X","X++"]
Output: 3
Explanation: The operations are performed as follows:
Initially, X = 0.
++X: X is incremented by 1, X = 0 + 1 = 1.
++X: X is incremented by 1, X = 1 + 1 = 2.
X++: X is incremented by 1, X = 2 + 1 = 3.

Example 3

1
2
3
4
5
6
7
8
Input: operations = ["X++","++X","--X","X--"]
Output: 0
Explanation:  The operations are performed as follows:
Initially, X = 0.
X++: X is incremented by 1, X = 0 + 1 = 1.
++X: X is incremented by 1, X = 1 + 1 = 2.
--X: X is decremented by 1, X = 2 - 1 = 1.
X--: X is decremented by 1, X = 1 - 1 = 0.

Constraints

  • 1 <= operations.length <= 100
  • operations[i] will be either "++X", "X++", "--X", or "X--".

Solution

Method 1 – Simple Simulation

Intuition

Each operation either increments or decrements the variable X by 1. We can simply iterate through the operations and update X accordingly.

Approach

  1. Initialize X to 0.
  2. For each operation in the list:
    • If the operation contains ‘+’, increment X by 1.
    • If the operation contains ‘-’, decrement X by 1.
  3. Return the final value of X.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
public:
    int finalValueAfterOperations(vector<string>& operations) {
        int ans = 0;
        for (auto& op : operations) {
            if (op.find('+') != string::npos) ans++;
            else ans--;
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
func finalValueAfterOperations(operations []string) int {
    ans := 0
    for _, op := range operations {
        if strings.Contains(op, "+") {
            ans++
        } else {
            ans--
        }
    }
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
    public int finalValueAfterOperations(String[] operations) {
        int ans = 0;
        for (String op : operations) {
            if (op.contains("+")) ans++;
            else ans--;
        }
        return ans;
    }
}
1
2
3
4
5
6
7
8
9
class Solution {
    fun finalValueAfterOperations(operations: Array<String>): Int {
        var ans = 0
        for (op in operations) {
            if ('+' in op) ans++ else ans--
        }
        return ans
    }
}
1
2
3
4
5
6
7
8
9
class Solution:
    def finalValueAfterOperations(self, operations: list[str]) -> int:
        ans = 0
        for op in operations:
            if '+' in op:
                ans += 1
            else:
                ans -= 1
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
impl Solution {
    pub fn final_value_after_operations(operations: Vec<String>) -> i32 {
        let mut ans = 0;
        for op in operations {
            if op.contains('+') {
                ans += 1;
            } else {
                ans -= 1;
            }
        }
        ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
    finalValueAfterOperations(operations: string[]): number {
        let ans = 0;
        for (const op of operations) {
            if (op.includes('+')) ans++;
            else ans--;
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(n), where n is the number of operations, as we process each operation once.
  • 🧺 Space complexity: O(1), only a variable for the answer is used.