SDE & MLE Prep

2390. Removing Stars From a String

Problem

Each * in s removes itself and the closest non-star character to its left. Return the string after processing every star (this is always possible).

Example: "leet**cod*e" → "lecoe"

Each * deletes the closest non-star character to its left — exactly a stack pop. Push letters, pop on *.

Complexity: O(n).

class Solution:
    def removeStars(self, s: str) -> str:
        st = []
        for ch in s:
            if ch == "*":
                st.pop()
            else:
                st.append(ch)
        return "".join(st)