Removing Stars From a String
Solution
- Use stack to keep track of characters seen so far
- If a star is seen and there was a character seen, remove from the stack
- In the end, join all remaining characters into a string and return the string
| Time | Space | Explanation |
|---|
O(n) | O(n) | |
def removeStars(self, s: str) -> str:
seen_stack = []
for ch in s:
if ch != '*':
seen_stack.append(ch)
else:
if seen_stack:
seen_stack.pop()
return ''.join(seen_stack)