Stack· 25 min

Removing Stars From a String

LeetCode #2390 · Medium · Interactive step-by-step visualization

Setup

Example inputs = "leet**cod*e"
Step 1 / 26

Current step

1 / 26

Read 'l'. Letters push onto the stack; a star removes the nearest surviving letter. Prepare to push.

Step log

s = "leet**cod*e"

Read
l
0
e
1
e
2
t
3
*
4
*
5
c
6
o
7
d
8
*
9
e
10

Stack · 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