Here is a breakdown of where your implementation leaked and how to tighten your methodology from a first-principles perspective.
Your biggest structural bug was letting states bleed into each other because of stacked if statements instead of if / else if / else chains.
When you are mapping out a problem, especially in DP or backtracking, every branch must be Mutually Exclusive and Collectively Exhaustive (MECE).
- Mutually Exclusive: If a string is
"*1", it cannot simultaneously be treated as a normal two-digit number. Only one path should mathematically be allowed to execute. - Collectively Exhaustive: You must cover every possible permutation (which is why forgetting the "two normal digits" scenario was a fatal error).
How to improve: Before touching the keyboard, draw out the decision tree on paper. Think of it like a strict state machine. If you enter State A, you cannot fall through to State B. Map the if/else boundaries a