Skip to main content

Removing Stars From a String

Medium
Data Structures and Algorithms
Editorial
Topics:
Stack
String
Simulation

You are given a string s, which contains stars *.

In one operation, you can:

  1. Choose a star in s.
  2. Remove the closest non-star character to its left, as well as remove the star itself.

Return the string after all stars have been removed.

Note:

  • The input is generated such that the operation is always possible.
  • It can be shown that the resulting string is always unique.

Example 1:

  • Input: s = "leet**cod*e"
  • Output: "lecoe"
  • Explanation:
  • Remove t with 1st *: lee*cod*e
  • Remove e with 2nd *: lecod*e
  • Remove d with 3rd *: lecoe

Example 2:

  • Input: s = "erase*****"
  • Output: ""

Constraints:

  • 1 <= s.length <= 10^5
  • s consists of lowercase English letters and stars *.
  • The operation above can be performed on s.

Need help or want more practice?

Practise in an interviewGet AI Coach help