All problems
0724EasyArrayStringSimulation

Hand Tally Reading After The Log

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2011Final Value of Variable After Performing Operations

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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