Back to Browse

Clear Digits — Stack Trick to Delete Chars & Digits Explained | LeetCode #3174 Full Walkthrough

4 views
May 14, 2026
4:42

Welcome back! In this video we solve LeetCode Problem #3174 — Clear Digits, a string simulation problem that introduces one of the most important and reusable data structures in coding interviews — the Stack — in one of its cleanest and most intuitive applications. The problem gives us a string containing both lowercase letters and digits. Our task is to repeatedly delete the first digit we find along with the closest non-digit character to its left, until no digits remain. The problem guarantees it's always possible to remove all digits. We need to return the resulting string after all deletions. We start by simulating the process by hand on the example "cb34". First we find the digit '3' at index 2 — its closest non-digit to the left is 'b' at index 1, so both are removed, giving us "c4". Now '4' is the first digit, and 'c' is to its left — both removed, leaving an empty string. Tracing this manually makes the pattern immediately clear. The naive approach of repeatedly scanning and modifying the string works but is slow — O(n²) in the worst case. The elegant solution is to use a Stack. We scan through the string once: if a character is a letter, push it onto the stack. If it's a digit, pop the top of the stack (removing the closest letter to the left) and discard the digit. At the end, the stack contains exactly the remaining letters in order.

Download

0 formats

No download links available.

Clear Digits — Stack Trick to Delete Chars & Digits Explained | LeetCode #3174 Full Walkthrough | NatokHD