2390. Removing Stars From a String
On This Page
You are provided with a string
s that contains asterisks or stars (*). Your task is to transform this string. In each operation, you’ll select a star and remove the character immediately to its left and the star itself. Continue this process until there are no stars left in the string.
The challenge guarantees that the input string can always be transformed as per the given operation, and the result will always be unique.
A direct approach would be to loop through the string, and every time you find a star, remove it along with the character to its left. Continue this process till no more stars exist. However, this can be optimized.
Hints & Tips
In Python, strings are immutable. This means, whenever you make a change to a string, a new string is created in memory. To circumvent this inefficiency, we can utilize a data structure like a list (or a stack in this case), which allows for mutable operations.
A simplified, yet efficient approach is to use a stack:
- Use the stack to store the characters of the string.
- As you iterate through the string, push characters onto the stack. If you come across a star, pop the top character from the stack (if the stack isn’t empty).
- The remaining characters in the stack after this operation will form the transformed string.
- Initialize an empty stack, named
- Traverse the string from the beginning.
- For each character:
- If the character is a star, pop the top character from the stack (provided the stack isn’t empty).
- Otherwise, push the character onto the stack.
- Convert the stack to a string to get the result.
def removeStars(s: str) -> str: stack =  for c in s: if c == '*': stack.pop() else: stack.append(c) return ''.join(stack)