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.
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--".Problem Overview: You start with a variable X = 0. An array of strings contains operations such as ++X, X++, --X, and X--. Each increment operation increases X by 1 and each decrement operation decreases it by 1. The task is to process the list of operations and return the final value after all updates.
Approach 1: Iterative Count Approach (Time: O(n), Space: O(1))
Traverse the operations array once and update a counter that represents X. For every string, check whether it represents an increment or decrement. A simple check like if '+' in operation means the value increases; otherwise it decreases. This is a direct simulation of the operations and mirrors exactly what the problem statement describes. Since you only scan the array once and keep a single integer variable, the time complexity is O(n) and the space complexity is O(1). This approach works in any language and is the most straightforward solution.
Approach 2: Using String Count Method (Time: O(n), Space: O(1))
Instead of updating the variable step-by-step, count how many increment and decrement operations exist. Each operation string always contains either '+' or '-'. By counting how many strings contain '+' and subtracting the number containing '-', you can compute the final result directly. Languages like Python and JavaScript allow concise implementations using string membership checks or array filtering. The algorithm still scans the list once, so the time complexity remains O(n) with O(1) additional space.
The problem mainly tests basic iteration over an array and simple string inspection. Conceptually, it is a small simulation task: apply each operation exactly as written and track the result.
Recommended for interviews: The iterative simulation approach is what interviewers typically expect. It demonstrates that you read the problem carefully and can translate the operations directly into code with O(n) time and constant space. The string-count variant is equally efficient but mostly a stylistic shortcut available in languages with convenient string operations.
This approach involves iterating over each operation in the given list and modifying the variable X based on the operation. We start with X initialized to 0, and for each operation, we either increment or decrement X accordingly.
The code initializes the variable X to 0. It then loops through each operation in the array. If the middle character of the operation (operations[i][1]) is a '+', it increments X. Otherwise, it decrements X. The final value of X is returned.
Time Complexity: O(n), where n is the number of operations.
Space Complexity: O(1), as only a fixed amount of extra space is used.
This approach counts the number of '+' and '-' operations directly. We count the times '++X' and 'X++' operations occur, and do the same for '--X' and 'X--'. The final value of X is calculated from the difference between the two counts.
This Python function uses the list's count method to calculate the number of increment and decrement operations separately. The final result is the difference between the counts of '+' and '-' operations.
Python
JavaScript
Time Complexity: O(n), where n is the number of operations.
Space Complexity: O(1), since the space is independent of input size.
We traverse the array operations. For each operation operations[i], if it contains '+', we increment the answer by 1, otherwise, we decrement the answer by 1.
The time complexity is O(n), where n is the length of the array operations. The space complexity is O(1).
Python
Java
C++
Go
TypeScript
Rust
JavaScript
C
| Approach | Complexity |
|---|---|
| Iterative Count Approach | Time Complexity: O(n), where n is the number of operations. |
| Using String Count Method | Time Complexity: O(n), where n is the number of operations. |
| Counting | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Count (Simulation) | O(n) | O(1) | Best general solution. Directly simulate each operation while scanning the array once. |
| String Count Method | O(n) | O(1) | Useful in Python/JavaScript for concise code using string membership or filtering. |
Final Value of Variable After Performing Operations | LeetCode 2011 | English • CodeClips with Abhishek Ranjan • 2,313 views views
Watch 9 more video solutions →Practice Final Value of Variable After Performing Operations with our built-in code editor and test cases.
Practice on FleetCode