Difficulty: Easy | Acceptance: 90.60% | Paid: No Topics: Array, String, Simulation
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.
- Examples
- Constraints
- Direct Simulation
- Hash Map Lookup
- Middle Character Check
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 <= 100
operations[i] will be either "++X", "X++", "--X", or "X--".
Direct Simulation
Intuition Iterate through each operation and check if it contains ”++” to increment, or ”—” to decrement the variable.
Steps
- Initialize X to 0
- For each operation in the array:
- If the operation contains ”++”, increment X by 1
- Otherwise (it contains ”—”), decrement X by 1
- Return the final value of X
python
class Solution:
def finalValueAfterOperations(self, operations: list[str]) -> int:
x = 0
for op in operations:
if '++' in op:
x += 1
else:
x -= 1
return xComplexity
- Time: O(n) where n is the number of operations
- Space: O(1) only using a constant amount of extra space
- Notes: Simple and straightforward approach
Hash Map Lookup
Intuition Create a mapping from each operation string to its corresponding value change, then sum up all the values.
Steps
- Create a hash map/dictionary with operations as keys and their values (+1 or -1) as values
- Initialize X to 0
- For each operation, add the corresponding value from the map to X
- Return the final value of X
python
class Solution:
def finalValueAfterOperations(self, operations: list[str]) -> int:
op_map = {'++X': 1, 'X++': 1, '--X': -1, 'X--': -1}
x = 0
for op in operations:
x += op_map[op]
return xComplexity
- Time: O(n) where n is the number of operations
- Space: O(1) the hash map has a constant size of 4 entries
- Notes: Slightly more overhead due to hash map lookups, but very clean code
Middle Character Check
Intuition Since all operations are exactly 3 characters long, the middle character (index 1) determines if we increment (+) or decrement (-).
Steps
- Initialize X to 0
- For each operation in the array:
- Check the middle character (index 1)
- If it’s ’+’, increment X by 1
- If it’s ’-’, decrement X by 1
- Return the final value of X
python
class Solution:
def finalValueAfterOperations(self, operations: list[str]) -> int:
x = 0
for op in operations:
if op[1] == '+':
x += 1
else:
x -= 1
return xComplexity
- Time: O(n) where n is the number of operations
- Space: O(1) only using a constant amount of extra space
- Notes: Most efficient approach as it only checks a single character