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--".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.
C++
Java
Python
C#
JavaScript
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.
JavaScript
Time Complexity: O(n), where n is the number of operations.
Space Complexity: O(1), since the space is independent of input size.
| 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. |
Final Value of Variable After Performing Operations | LeetCode 2011 | English • CodeClips with Abhishek Ranjan • 2,052 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