FunctionRacerProblemsRace

Pairs That Make Ten

EasyStacks and queuesPython, JavaScript, TypeScript, Java, C++

digits is a string of digits 0 to 9. Whenever two neighbouring digits add up to exactly 10 (such as 3 and 7, or 5 and 5), both are removed, and the digits on either side of the gap become neighbours. Keep removing until no two neighbouring digits add up to 10.

Return the digits that remain, in their original order, as a string (possibly empty). The final string does not depend on which pair you remove first.

Example 1

digits "1937"
returns ""

1 and 9 make ten and go; then 3 and 7 make ten and go. Nothing is left.

Example 2

digits "2805"
returns "05"

2 and 8 go; 0 and 5 do not add up to ten, so they stay.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Stacks and queues problems