Final Value of Variable After Performing Operations

There is a programming language with only four operations and one variable X: ++X and X++ increment X by 1, while --X and X-- decrement X by 1. X starts at 0. Given an array of strings operations, return the final value of X after performing all the operations.

30:00

Final Value of Variable After Performing Operations
easy
Free written solution
Topics
Companies

There is a programming language with only four operations and one variable X: ++X and X++ increment X by 1, while --X and X-- decrement X by 1. X starts at 0. Given an array of strings operations, return the final value of X after performing all the operations.

Example 1:
Input: {"operations":["--X","X++","X++"]}
Output: 1
Constraints:
  • 1≤operations.length≤1001 \leq \text{operations.length} \leq 100

  • operations[i] will be either "++X", "X++", "--X", or "X--".

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"operations":["--X","X++","X++"]}

X starts at 0. There are 3 operations to apply, and only the middle character of each one matters.

operations (only the middle character counts)

--X

0

X++

1

X++

2

X

0

0/3 applied

X over time

-1

0

1

Variables
VariableValue
x0
n3
DepthFunction Call
Stack empty
0/7