Loading...
Loading...
You are given a string commands that lists operations on a counter. The counter starts at 0. There are three commands:
Inc adds 1 to the counter.Dec subtracts 1 from the counter.Reset sets the counter back to 0.A group is a part of the string wrapped in parentheses and followed by a positive integer k, its count. It means the commands inside the parentheses run k times in a row. For example, (IncDec)3 is the same as IncDecIncDecIncDec. Groups can be nested inside other groups.
Every group has a count, even when the count is 1. Commands outside every group run once.
Return the value of the counter after all the commands have run.
Input: commands = "Inc(ResetDec)2"
Output: -1
Explanation: Written out in full, the commands are Inc Reset Dec Reset Dec.
The counter goes 1, 0, -1, 0, -1.
Input: commands = "IncInc(Inc)3Dec"
Output: 4
Explanation: The two Inc commands make the counter 2. The group runs Inc 3 times, giving 5.
The final Dec leaves 4.
Input: commands = "(Inc(Dec)2)1000000000"
Output: -1000000000
Explanation: One run of the outer group is Inc Dec Dec, which lowers the counter by 1.
The group runs 1000000000 times, so the counter ends at -1000000000.
Input: commands = "Inc(IncReset)5Inc"
Output: 1
Explanation: Every run of the group ends with Reset, so the counter is 0 after the group.
The last Inc makes it 1.
commands.length ≤105commands consists only of the words Inc, Dec and Reset, the characters ( and ), and digits.) is directly followed by its count, an integer from 1 to 109 with no leading zeros. Digits appear nowhere else.commands has at most 1015 commands.