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 <= 100
operations[i]
will be either "++X"
, "X++"
, "--X"
, or "X--"
.This problem involves simulating a simple programming language with operations that increment or decrement a variable X. The goal is to determine the final value of X after executing a sequence of operations.
The most efficient way to solve this is by iterating through the array of operations and directly updating a counter representing the value of X. We don't need to explicitly track X; we can simply track the net change.
Initialization: Start with a variable X
(or ans
in the code examples) initialized to 0.
Iteration: Loop through each operation string in the input array operations
.
Conditional Increment/Decrement: Examine the second character of the operation string (index 1).
'+'
, increment X
by 1.'-'
, decrement X
by 1.Return Value: After processing all operations, the final value of X
is the solution.
Time Complexity: O(n), where n is the length of the operations
array. We iterate through the array once.
Space Complexity: O(1). We only use a constant amount of extra space to store the variable X
and loop counters.
The provided code in multiple languages all implement the same core logic. Here's a breakdown focusing on the Python example, as it's concise and easy to read:
class Solution:
def finalValueAfterOperations(self, operations: List[str]) -> int:
return sum(1 if s[1] == '+' else -1 for s in operations)
Explanation:
class Solution:
: Defines a class named Solution
(standard LeetCode convention).def finalValueAfterOperations(self, operations: List[str]) -> int:
: This defines a method within the class. It takes a list of strings (operations
) as input and returns an integer representing the final value of X. The type hints (List[str]
, -> int
) are for clarity.return sum(1 if s[1] == '+' else -1 for s in operations)
: This is a highly efficient one-liner.
for s in operations
: This iterates over each operation string in the input list.1 if s[1] == '+' else -1
: This is a conditional expression. It checks the second character (s[1]
) of the string. If it's '+'
, it evaluates to 1 (increment); otherwise, it evaluates to -1 (decrement).sum(...)
: This sums up the increments and decrements, effectively calculating the final value of X.The other language examples use similar logic, with minor syntax differences to accommodate the specific language's features. They all maintain the O(n) time complexity and O(1) space complexity.