Final Value of Variable After Operations

Easy

Description

There is a programming language with only four operations: ++X, X++, --X, X--. Initially X=0. Given an array of operations, return the final value of X.

Examples

Input:operations = ["--X","X++","X++"]
Output:1
Explanation:

-1 +1 +1 = 1.

Input:operations = ["X++","++X","--X","X--"]
Output:0
Explanation:

Two increments (+1, +1) and two decrements (-1, -1) cancel out: 0 + 1 + 1 - 1 - 1 = 0.

Input:operations = ["++X","--X","++X","X--","X++"]
Output:1
Explanation:

Starting at 0: ++X gives 1, --X gives 0, ++X gives 1, X-- gives 0, X++ gives 1. Final result: 1.

Constraints

  • 1 ≤ operations.length ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!