|
| 1 | +# 2390. Removing Stars From a String |
| 2 | + |
| 3 | +> Difficulty: Medium |
| 4 | +> Source: [LeetCode](https://leetcode.com/problems/removing-stars-from-a-string) |
| 5 | +> Tags: Stack, String, Simulation |
| 6 | +
|
| 7 | +## Description |
| 8 | + |
| 9 | +You are given a string `s`, which contains stars `*`. |
| 10 | + |
| 11 | +In one operation, you can: |
| 12 | +- Choose a star in `s`. |
| 13 | +- Remove the closest **non-star** character to its **left**, as well as remove the star itself. |
| 14 | + |
| 15 | +Return *the string after all stars have been removed*. |
| 16 | + |
| 17 | +**Note:** |
| 18 | +- The input will be generated such that the operation is always possible. |
| 19 | +- It can be shown that the resulting string will always be unique. |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## Examples |
| 24 | + |
| 25 | +### Example 1: |
| 26 | +``` |
| 27 | +Input: s = "leet**cod*e" |
| 28 | +Output: "lecoe" |
| 29 | +Explanation: Performing the removals from left to right: |
| 30 | +- The closest character to the 1st star is 't' -> "lee*cod*e" |
| 31 | +- Then remove 'e' -> "lecod*e" |
| 32 | +- Then remove 'd' -> "lecoe" |
| 33 | +``` |
| 34 | + |
| 35 | +### Example 2: |
| 36 | +``` |
| 37 | +Input: s = "erase*****" |
| 38 | +Output: "" |
| 39 | +Explanation: All characters are removed. |
| 40 | +``` |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +## Constraints |
| 45 | + |
| 46 | +- `1 <= s.length <= 10⁵` |
| 47 | +- `s` consists of lowercase English letters and stars `*`. |
| 48 | +- The operation above can be performed on `s`. |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +## Solution |
| 53 | + |
| 54 | +### Approach: Stack Simulation |
| 55 | + |
| 56 | +We simulate the removal process using a stack: |
| 57 | +- Iterate through the string `s`. |
| 58 | +- Push characters onto a stack until you encounter a `*`. |
| 59 | +- When a `*` is found, pop the top character from the stack (removing the left character). |
| 60 | + |
| 61 | +### Time and Space Complexity |
| 62 | + |
| 63 | +- **Time Complexity:** O(n) |
| 64 | +- **Space Complexity:** O(n) in the worst case (when no `*` are present) |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +## Code |
| 69 | + |
| 70 | +### Python |
| 71 | + |
| 72 | +```python |
| 73 | +class Solution: |
| 74 | + def removeStars(self, s: str) -> str: |
| 75 | + ans = [] |
| 76 | + for c in s: |
| 77 | + if c == '*': |
| 78 | + ans.pop() |
| 79 | + else: |
| 80 | + ans.append(c) |
| 81 | + return ''.join(ans) |
| 82 | +``` |
| 83 | + |
| 84 | +### Java |
| 85 | + |
| 86 | +```java |
| 87 | +class Solution { |
| 88 | + public String removeStars(String s) { |
| 89 | + StringBuilder ans = new StringBuilder(); |
| 90 | + for (char c : s.toCharArray()) { |
| 91 | + if (c == '*') { |
| 92 | + ans.deleteCharAt(ans.length() - 1); |
| 93 | + } else { |
| 94 | + ans.append(c); |
| 95 | + } |
| 96 | + } |
| 97 | + return ans.toString(); |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +### C++ |
| 103 | + |
| 104 | +```cpp |
| 105 | +class Solution { |
| 106 | +public: |
| 107 | + string removeStars(string s) { |
| 108 | + string ans; |
| 109 | + for (char c : s) { |
| 110 | + if (c == '*') { |
| 111 | + ans.pop_back(); |
| 112 | + } else { |
| 113 | + ans.push_back(c); |
| 114 | + } |
| 115 | + } |
| 116 | + return ans; |
| 117 | + } |
| 118 | +}; |
| 119 | +``` |
| 120 | +
|
| 121 | +### Go |
| 122 | +
|
| 123 | +```go |
| 124 | +func removeStars(s string) string { |
| 125 | + ans := []rune{} |
| 126 | + for _, c := range s { |
| 127 | + if c == '*' { |
| 128 | + ans = ans[:len(ans)-1] |
| 129 | + } else { |
| 130 | + ans = append(ans, c) |
| 131 | + } |
| 132 | + } |
| 133 | + return string(ans) |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +### TypeScript |
| 138 | + |
| 139 | +```ts |
| 140 | +function removeStars(s: string): string { |
| 141 | + const ans: string[] = []; |
| 142 | + for (const c of s) { |
| 143 | + if (c === '*') { |
| 144 | + ans.pop(); |
| 145 | + } else { |
| 146 | + ans.push(c); |
| 147 | + } |
| 148 | + } |
| 149 | + return ans.join(''); |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +### Rust |
| 154 | + |
| 155 | +```rust |
| 156 | +impl Solution { |
| 157 | + pub fn remove_stars(s: String) -> String { |
| 158 | + let mut ans = String::new(); |
| 159 | + for c in s.chars() { |
| 160 | + if c == '*' { |
| 161 | + ans.pop(); |
| 162 | + } else { |
| 163 | + ans.push(c); |
| 164 | + } |
| 165 | + } |
| 166 | + ans |
| 167 | + } |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +### PHP |
| 172 | + |
| 173 | +```php |
| 174 | +class Solution { |
| 175 | + function removeStars($s) { |
| 176 | + $stack = []; |
| 177 | + for ($i = 0; $i < strlen($s); $i++) { |
| 178 | + if ($s[$i] === '*') { |
| 179 | + array_pop($stack); |
| 180 | + } else { |
| 181 | + $stack[] = $s[$i]; |
| 182 | + } |
| 183 | + } |
| 184 | + return implode('', $stack); |
| 185 | + } |
| 186 | +} |
| 187 | +``` |
| 188 | + |
0 commit comments