Show problems as
Classic problem #2390MediumStringStackSimulation

Removing Stars From a String

Real-world version: Rubbing Letters Off the Tape
Tracked in this browser only
Read Removing Stars From a String on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A tape tape holds lowercase letters and the character *.

Working from left to right, each * rubs itself out along with the nearest letter still standing to its left.

Return what the tape reads once every * has been dealt with. The tape always leaves a letter for each * to rub out.

Examples

Example 1

Input
tape = "abc*d*e"
Output
"abe"

The first star rubs out the c and the second rubs out the d, leaving the a, the b and the e.

Example 2

Input
tape = "aaa***"
Output
""

Each of the three stars rubs out one of the three letters, so nothing is left.

Example 3

Input
tape = "xy*z"
Output
"xz"

The star rubs out the y, and the x and the z close up together.

Constraints

  • 1 <= tape.length <= 10^5
  • every character of tape is a lowercase English letter or *
  • the tape always leaves a letter for each star to rub out

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def remove_stars(tape: str) -> str:
Java
public String removeStars(String tape)