Lexicographically Minimum String After Removing Stars - Problem

You're given a string s containing lowercase letters and asterisks (*). Your mission is to remove all asterisks by following a specific elimination rule that will give you the lexicographically smallest possible result.

The Elimination Rule: For each asterisk (*) you encounter:

  • Find the leftmost asterisk
  • Look to its left and find the smallest character (lexicographically)
  • Remove both the asterisk and that smallest character
  • If there are multiple smallest characters, you can remove any one of them

Goal: Return the lexicographically smallest string after removing all asterisks.

Example: In "abc*de*", the first * removes 'a' (smallest to its left), and the second * removes 'b', leaving "cde".

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "leet*cod*e"
โ€บ Output: "letcode"
๐Ÿ’ก Note: First '*' removes smallest char to its left ('e' from 'leet'), leaving 'let*cod*e'. Second '*' removes 't', leaving 'lecod*e'. Last '*' removes 'c', giving final result 'letcode'.
example_2.py โ€” Multiple Same Characters
$ Input: s = "erase*****"
โ€บ Output: ""
๐Ÿ’ก Note: Each '*' removes one character: 'erase****' โ†’ 'rase***' โ†’ 'ase**' โ†’ 'se*' โ†’ 'e' โ†’ ''. All characters are eliminated.
example_3.py โ€” No Stars
$ Input: s = "abc"
โ€บ Output: "abc"
๐Ÿ’ก Note: No asterisks present, so the string remains unchanged.

Constraints

  • 1 โ‰ค s.length โ‰ค 105
  • s consists of lowercase English letters and '*' characters
  • It is guaranteed that the string can be processed (no asterisk without preceding characters)

Visualization

Tap to expand
Lexicographically Minimum String After Removing StarsInput: "leet*cod*e"leet*cod*eStack Processing:Step 1: Add 'l', 'e', 'e', 't' to stackStep 2: '*' removes smallest ('e')Step 3: Add 'c', 'o', 'd' to stackStep 4: '*' removes smallest ('c')Step 5: Add 'e', collect resultStack State After First '*':leetโ† removedStack State After Adding 'c', 'o', 'd':leetcodAfter Second '*' (removes 'c'):leetcodeFinal Result (collect non-removed):letcode"letcode"๐Ÿ’ก Key InsightUsing a min-heap allows us tofind the smallest character inO(log n) time instead of O(n).The stack preserves originalorder while we mark charactersfor removal efficiently.Time: O(n log n), Space: O(n)
Understanding the Visualization
1
Add to Stack
Place each character on the stack as you encounter it
2
Track in Heap
Also add each character to a min-heap for quick smallest-element access
3
Process Asterisk
When you see *, remove the smallest character from heap and mark its stack position
4
Build Result
Collect all non-removed characters from the stack in their original order
Key Takeaway
๐ŸŽฏ Key Insight: The combination of stack (for order preservation) and min-heap (for efficient minimum finding) gives us the optimal balance between correctness and performance.
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 22
78.5K Views
Medium-High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen