Final Value of Variable After Performing Operations

There is a programming language with only four operations and one variable X:

  • ++X and X++ increment the value of the variable X by 1.
  • --X and X-- decrement 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
Inputoperations = ["--X","X++","X++"]
Output1
Starting from 0, --X makes X equal to -1, and the two X++ operations bring it to 1.
Example 2
Inputoperations = ["++X","++X","X++"]
Output3
Starting from 0, each of the three increment operations increases X by 1, so the final value is 3.

Constraints

  • 1 <= operations.length <= 100
  • operations[i] will be either "++X", "X++", "--X", or "X--".

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate