< Back





2390. Removing Stars from a String

This problem is similar to 844. Backspace String Compare. We're given a string
and asked to remove all asterisks and adjacent characters. We return the string after all asterisks
are removed.

The solution is as follows:


  class Solution:
      def removeStars(self, s: str) -> str:
          ans = []

          for c in s:
              if c == '*' and ans:
                  ans.pop()
              else:
                  ans.append(c)

          return "".join(ans)


_ Time Complexity:

  O(n) - We inspect all characters in the string.

_ Space Complexity:

  O(n) - We maintain a stack to create the result.