Removing Stars From a String - Problem

You are given a string s containing letters and stars (*). Your task is to simulate a backspace operation where each star removes the closest non-star character to its left.

The Process:

  • When you encounter a star *, remove the closest non-star character to its left
  • Also remove the star itself
  • Repeat until all stars are processed

Goal: Return the final string after all stars have been removed and processed.

Note: The input guarantees that every star will have at least one non-star character to its left, so the operation is always valid.

Example: "abc*de*f" becomes "adf" because the first * removes 'c', and the second * removes 'e'.

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "leet**cod*e"
โ€บ Output: "lecoe"
๐Ÿ’ก Note: Processing left to right: 'l','e','e','t' get added, first '*' removes 't', second '*' removes 'e', then 'c','o','d' get added, third '*' removes 'd', finally 'e' gets added. Result: "lecoe"
example_2.py โ€” Simple Case
$ Input: s = "erase*****"
โ€บ Output: ""
๐Ÿ’ก Note: The string 'erase' has 5 characters, and there are 5 stars. Each star removes one character from left to right: 'e' removed, 'r' removed, 'a' removed, 's' removed, 'e' removed. Final result is empty string.
example_3.py โ€” No Stars
$ Input: s = "abc"
โ€บ Output: "abc"
๐Ÿ’ก Note: No stars in the string, so no characters are removed. The result is the original string "abc".

Visualization

Tap to expand
Stack-Based Star Removal SimulationText Editor Buffer (Stack)Characters are added to buffer, stars (backspace) remove the most recent characterStep-by-Step Process for "abc*d"Input: aPushaInput: bPushabInput: cPushabcInput: *PopabcInput: dPushabdFinal Result"abd"Stack contents joined together๐Ÿ’ก Key Insight: Stack perfectly simulates the "backspace" behavior - O(n) time, single pass!
Understanding the Visualization
1
Initialize Stack
Start with an empty stack to represent the text buffer
2
Process Characters
For each regular character, add it to the buffer (push to stack)
3
Handle Backspaces
For each star (*), remove the most recent character (pop from stack)
4
Build Result
The final stack contents represent the text after all backspaces
Key Takeaway
๐ŸŽฏ Key Insight: The stack data structure perfectly mimics a text editor's buffer, making this an elegant and efficient solution that processes each character exactly once.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the string, each character is processed once

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Stack can contain at most n characters in worst case (no stars)

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค s.length โ‰ค 105
  • s consists of lowercase English letters and stars *
  • The given string is valid (every star has a non-star character to its left)
Asked in
Amazon 35 Microsoft 28 Google 22 Meta 18
68.5K Views
Medium-High Frequency
~12 min Avg. Time
2.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