Skip to content

Removing Stars From a String

MediumStack
Decode it first. Clues, constraints, pattern and plan before any code.

You get a string s of lowercase letters and stars *. Each star works like a backspace: it deletes the closest non-star character to its left, and the star itself disappears too. Apply every star and return the string that is left.

s = "leet**cod*e"  →  "lecoe"
s = "erase*****"   →  ""

In the first example the two stars remove t and e, and the third removes d.

Constraints

  • 1 <= s.length <= 10^5
  • s has only lowercase letters and *.
  • Every star always has a character to delete.