Removing Stars From a String
Real-world version: Rubbing Letters Off the TapeThe 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.
def remove_stars(tape: str) -> str:public String removeStars(String tape)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Backspace String CompareReal-world version: Strike Marks On Two TapesEasy
- Minimum String Length After Removing SubstringsReal-world version: Cancelling Paint MarksEasy
- Remove All Occurrences of a SubstringReal-world version: Cut the Marker from the TapeMedium
- Valid ParenthesesReal-world version: Template Grouping CheckEasy