Stop the loop early
transcript
Cut digits into amounts. Think of a bill line of digits. The pieces must add to 39. Most cuts fail early. Stop them as soon as you can.
The first piece can be one, twelve or 123. Each piece is one choice. One and twelve fit. One hundred twenty-three is too big.
A longer piece is a bigger number. So none can fit. No deeper call is needed. Stop, do not skip.
A leading zero is different. Rules differ in how far they reach. It also ends the loop. A palindrome test only skips.
A piece may not start with zero. Zero is not an amount. Even a single zero is out. So end the loop right there. No later piece can help.
With the stop, 25,094 calls. Without it, 65,536. The answer is the same.
Reject at the first sign. A bigger piece cannot help. Then move to the next start. Stop the loop. Full breakdown in the lecture.