Show problems as
Classic problem #2011EasyArrayStringSimulation

Final Value of Variable After Performing Operations

Real-world version: Hand Tally Reading After The Log
Tracked in this browser only
Read Final Value of Variable After Performing Operations on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A stock clerk carries a hand tally whose reading starts at 0. Every adjustment is written into a log as one of four three-character tokens naming the tally n:

  • "++n" and "n++" each raise the reading by one;
  • "--n" and "n--" each lower it by one.

Whether the clerk wrote the pair of signs before or after the letter makes no difference to what happens to the reading.

taps holds the log entries in the order they were made. Return the reading once every entry has been applied. The reading may end up negative.

Examples

Example 1

Input
taps = ["--n", "n++", "n++"]
Output
1

The reading starts at 0, drops to -1, then rises to 0 and to 1.

Example 2

Input
taps = ["n--", "n--", "--n"]
Output
-3

All three entries lower the reading, so it runs 0, -1, -2, -3.

Example 3

Input
taps = ["++n"]
Output
1

A single rise takes the reading from 0 to 1.

Constraints

  • 1 <= taps.length <= 100
  • taps[i].length == 3
  • Every entry of taps is one of "++n", "n++", "--n" or "n--".

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def counter_reading(taps: list[str]) -> int:
Java
public int counterReading(String[] taps)