Removing Stars From a String

You are given a string `s` made of lowercase letters and stars `*`. In one operation you pick a star, then delete it together with the closest non-star character to its left. Return the string after every star has been removed. The input always allows this, and the result is unique.

30:00

Removing Stars From a String
medium
Free written solution
Topics
Companies

You are given a string s made of lowercase letters and stars *. In one operation you pick a star, then delete it together with the closest non-star character to its left. Return the string after every star has been removed. The input always allows this, and the result is unique.

Example 1:
Input: {"s":"leet**cod*e"}
Output: lecoe
Constraints:
  • 1≤s.length≤1051 \leq s.\text{length} \leq 10^5

  • s consists of lowercase English letters and stars *.

  • The operation above can be performed on s.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"leet**cod*e"}

s; a star and the letter it erased share a colour

l

0

e

1

e

2

t

3

*

4

*

5

c

6

o

7

d

8

*

9

e

10

Scan left to right. Letters go on a stack; the top is always the closest surviving letter to the left of the next character.

stack of surviving letters (bottom → top)

size 0

empty

stars used 0
chars read 0 / 11
Variables
VariableValue
stack
starsUsed0
DepthFunction Call
Stack empty
0/23