Final Value of Variable After Performing Operations
EasyUpdated: Aug 2, 2025
Practice on:
Problem
There is a programming language with only four operations and one variable X:
++XandX++increments the value of the variableXby1.--XandX--decrements the value of the variableXby1.
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
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
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
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 <= 100operations[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
- Initialize X to 0.
- For each operation in the list:
- If the operation contains '+', increment X by 1.
- If the operation contains '-', decrement X by 1.
- Return the final value of X.
Code
C++
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;
}
};
Go
func finalValueAfterOperations(operations []string) int {
ans := 0
for _, op := range operations {
if strings.Contains(op, "+") {
ans++
} else {
ans--
}
}
return ans
}
Java
class Solution {
public int finalValueAfterOperations(String[] operations) {
int ans = 0;
for (String op : operations) {
if (op.contains("+")) ans++;
else ans--;
}
return ans;
}
}
Kotlin
class Solution {
fun finalValueAfterOperations(operations: Array<String>): Int {
var ans = 0
for (op in operations) {
if ('+' in op) ans++ else ans--
}
return ans
}
}
Python
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
Rust
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
}
}
TypeScript
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.