Stack· 25 min
Removing Stars From a String
LeetCode #2390 · Medium · Interactive step-by-step visualization
Setup
Example input
s = "leet**cod*e"Step 1 / 26
Current step
1 / 26Read 'l'. Letters push onto the stack; a star removes the nearest surviving letter. Prepare to push.
Step log
s = "leet**cod*e"
Read
l
0e
1e
2t
3*
4*
5c
6o
7d
8*
9e
10Stack · top on right
Active cursor
Comparing
Best or completed
Leaving or removed
removing-stars-from-a-string.java
1public String removeStars(String s) {2 StringBuilder stack = new StringBuilder();3 for (char character : s.toCharArray()) {4 if (character == '*')5 stack.deleteCharAt(stack.length() - 1);6 else stack.append(character);7 }8 return stack.toString();9}● Java
Time
O(n)
Space
O(n)
Pattern
stack
Frames
26
