|
| 1 | +--- |
| 2 | +id: lexicographically-minimum-string-after-removing-stars |
| 3 | +title: Lexicographically Minimum String After Removing Stars |
| 4 | +sidebar_label: 3170 . Lexicographically Minimum String After Removing Stars |
| 5 | +tags: |
| 6 | +- Hash Table |
| 7 | +- String |
| 8 | +- Stack |
| 9 | +- Greedy |
| 10 | +- Heap (Priority Queue) |
| 11 | +description: "This is a solution to the Lexicographically Minimum String After Removing Stars problem on LeetCode." |
| 12 | +--- |
| 13 | + |
| 14 | +## Problem Description |
| 15 | +You are given a string s. It may contain any number of '*' characters. Your task is to remove all '*' characters. |
| 16 | + |
| 17 | +While there is a '*', do the following operation: |
| 18 | + |
| 19 | +Delete the leftmost '*' and the smallest non-'*' character to its left. If there are several smallest characters, you can delete any of them. |
| 20 | +Return the lexicographically smallest resulting string after removing all '*' characters. |
| 21 | + |
| 22 | +### Examples |
| 23 | + |
| 24 | +**Example 1:** |
| 25 | + |
| 26 | +``` |
| 27 | +Input: s = "aaba*" |
| 28 | +
|
| 29 | +Output: "aab" |
| 30 | +
|
| 31 | +Explanation: |
| 32 | +
|
| 33 | +We should delete one of the 'a' characters with '*'. If we choose s[3], s becomes the lexicographically smallest. |
| 34 | +``` |
| 35 | + |
| 36 | +**Example 2:** |
| 37 | +``` |
| 38 | +Input: s = "abc" |
| 39 | +
|
| 40 | +Output: "abc" |
| 41 | +
|
| 42 | +Explanation: |
| 43 | +
|
| 44 | +There is no '*' in the string. |
| 45 | +``` |
| 46 | + |
| 47 | +### Constraints |
| 48 | + |
| 49 | +- `1 <= s.length <= 10^5` |
| 50 | +- `s consists only of lowercase English letters and '*'.` |
| 51 | +- `The input is generated such that it is possible to delete all '*' characters.` |
| 52 | + |
| 53 | +## Solution for Lexicographically Minimum String After Removing Stars Problem |
| 54 | +### Approach |
| 55 | + |
| 56 | +#### Initialization: |
| 57 | + |
| 58 | +- We initialize two sets: |
| 59 | +- st: A set to store pairs of (character value, negative index). This helps us quickly identify and remove the leftmost character when we encounter a star. |
| 60 | +- del: A set to keep track of the indices of characters that need to be deleted (either because they are stars or they were removed by stars). |
| 61 | +#### Traversing the String: |
| 62 | +- We iterate over the string from left to right. |
| 63 | +#### For each character: |
| 64 | +- If the character is a star (*): |
| 65 | +- We identify the leftmost character in the set st by taking the smallest element (due to the ordering by character value and then by index). |
| 66 | +- We remove this leftmost character from the set st. |
| 67 | +- We record the indices of this leftmost character and the star itself in the del set. |
| 68 | +- If the character is not a star: |
| 69 | +- We add a pair of (character value, negative index) to the set st. |
| 70 | +#### Building the Result: |
| 71 | + |
| 72 | +- We initialize an empty string ans to store the result. |
| 73 | +- We iterate over the original string again. |
| 74 | +- For each character: |
| 75 | +- If the index of the character is not in the del set (i.e., it hasn't been marked for deletion), we append it to ans. |
| 76 | + |
| 77 | +<Tabs> |
| 78 | + <TabItem value="Solution" label="Solution"> |
| 79 | + |
| 80 | + #### Implementation |
| 81 | + ```jsx live |
| 82 | + function Solution(arr) { |
| 83 | + var clearStars = function(s) { |
| 84 | + const mp = new Map(); |
| 85 | + const n = s.length; |
| 86 | + const v = new Array(n).fill(0); |
| 87 | + |
| 88 | + for (let i = 0; i < n; i++) { |
| 89 | + if (s[i] !== '*') { |
| 90 | + if (!mp.has(s[i])) { |
| 91 | + mp.set(s[i], []); |
| 92 | + } |
| 93 | + mp.get(s[i]).push(i); |
| 94 | + } else { |
| 95 | + v[i] = 1; |
| 96 | + const sortedEntries = Array.from(mp.entries()).sort((a, b) => a[0].localeCompare(b[0])); |
| 97 | + for (let [key, indices] of sortedEntries) { |
| 98 | + const m = indices.length; |
| 99 | + v[indices[m - 1]] = 1; |
| 100 | + indices.pop(); |
| 101 | + if (indices.length === 0) { |
| 102 | + mp.delete(key); |
| 103 | + } |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + let ans = ""; |
| 110 | + for (let i = 0; i < n; i++) { |
| 111 | + if (v[i] !== 1) { |
| 112 | + ans += s[i]; |
| 113 | + } |
| 114 | + } |
| 115 | + return ans; |
| 116 | +}; |
| 117 | + const input = "aaba*" |
| 118 | + const output =clearStars(input) |
| 119 | + return ( |
| 120 | + <div> |
| 121 | + <p> |
| 122 | + <b>Input: </b> |
| 123 | + {JSON.stringify(input)} |
| 124 | + </p> |
| 125 | + <p> |
| 126 | + <b>Output:</b> {output.toString()} |
| 127 | + </p> |
| 128 | + </div> |
| 129 | + ); |
| 130 | + } |
| 131 | + ``` |
| 132 | + |
| 133 | + #### Complexity Analysis |
| 134 | + |
| 135 | + - Time Complexity: $ O(nlogn) $ because of Nested Loops |
| 136 | + - Space Complexity: $ O(n) $ because of prefix array |
| 137 | + |
| 138 | + ## Code in Different Languages |
| 139 | + <Tabs> |
| 140 | + <TabItem value="JavaScript" label="JavaScript"> |
| 141 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 142 | + ```javascript |
| 143 | +var clearStars = function(s) { |
| 144 | + const mp = new Map(); |
| 145 | + const n = s.length; |
| 146 | + const v = new Array(n).fill(0); |
| 147 | + |
| 148 | + for (let i = 0; i < n; i++) { |
| 149 | + if (s[i] !== '*') { |
| 150 | + if (!mp.has(s[i])) { |
| 151 | + mp.set(s[i], []); |
| 152 | + } |
| 153 | + mp.get(s[i]).push(i); |
| 154 | + } else { |
| 155 | + v[i] = 1; |
| 156 | + const sortedEntries = Array.from(mp.entries()).sort((a, b) => a[0].localeCompare(b[0])); |
| 157 | + for (let [key, indices] of sortedEntries) { |
| 158 | + const m = indices.length; |
| 159 | + v[indices[m - 1]] = 1; |
| 160 | + indices.pop(); |
| 161 | + if (indices.length === 0) { |
| 162 | + mp.delete(key); |
| 163 | + } |
| 164 | + break; |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + let ans = ""; |
| 170 | + for (let i = 0; i < n; i++) { |
| 171 | + if (v[i] !== 1) { |
| 172 | + ans += s[i]; |
| 173 | + } |
| 174 | + } |
| 175 | + return ans; |
| 176 | +}; |
| 177 | + ``` |
| 178 | + |
| 179 | + </TabItem> |
| 180 | + <TabItem value="TypeScript" label="TypeScript"> |
| 181 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 182 | + ```typescript |
| 183 | + function clearStars(s: string): string { |
| 184 | + const n = s.length; |
| 185 | + const st: Set<[number, number]> = new Set(); |
| 186 | + const del: Set<number> = new Set(); |
| 187 | + for (let i = 0; i < n; i++) { |
| 188 | + if (s[i] === '*') { |
| 189 | + const first = Array.from(st)[0]; |
| 190 | + st.delete(first); |
| 191 | + del.add(-first[1]); |
| 192 | + del.add(i); |
| 193 | + } else { |
| 194 | + st.add([s.charCodeAt(i) - 'a'.charCodeAt(0), -i]); |
| 195 | + } |
| 196 | + } |
| 197 | +
|
| 198 | + let ans = ''; |
| 199 | + for (let i = 0; i < n; i++) { |
| 200 | + if (!del.has(i)) ans += s[i]; |
| 201 | + } |
| 202 | + return ans; |
| 203 | +} |
| 204 | +
|
| 205 | + ``` |
| 206 | + </TabItem> |
| 207 | + <TabItem value="Python" label="Python"> |
| 208 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 209 | + ```python |
| 210 | + def clearStars(s: str) -> str: |
| 211 | + n = len(s) |
| 212 | + st = set() |
| 213 | + del_set = set() |
| 214 | + |
| 215 | + for i in range(n): |
| 216 | + if s[i] == '*': |
| 217 | + first = min(st) |
| 218 | + st.remove(first) |
| 219 | + del_set.add(-first[1]) |
| 220 | + del_set.add(i) |
| 221 | + else: |
| 222 | + st.add((ord(s[i]) - ord('a'), -i)) |
| 223 | + |
| 224 | + ans = ''.join(s[i] for i in range(n) if i not in del_set) |
| 225 | + return ans |
| 226 | +
|
| 227 | + ``` |
| 228 | + </TabItem> |
| 229 | + <TabItem value="Java" label="Java"> |
| 230 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 231 | + ```java |
| 232 | + import java.util.*; |
| 233 | +
|
| 234 | +public class Solution { |
| 235 | + public String clearStars(String s) { |
| 236 | + int n = s.length(); |
| 237 | + TreeSet<Pair> st = new TreeSet<>((a, b) -> a.first == b.first ? a.second - b.second : a.first - b.first); |
| 238 | + Set<Integer> del = new HashSet<>(); |
| 239 | + |
| 240 | + for (int i = 0; i < n; i++) { |
| 241 | + if (s.charAt(i) == '*') { |
| 242 | + Pair first = st.first(); |
| 243 | + st.remove(first); |
| 244 | + del.add(-first.second); |
| 245 | + del.add(i); |
| 246 | + } else { |
| 247 | + st.add(new Pair(s.charAt(i) - 'a', -i)); |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + StringBuilder ans = new StringBuilder(); |
| 252 | + for (int i = 0; i < n; i++) { |
| 253 | + if (!del.contains(i)) ans.append(s.charAt(i)); |
| 254 | + } |
| 255 | + return ans.toString(); |
| 256 | + } |
| 257 | +
|
| 258 | + class Pair { |
| 259 | + int first, second; |
| 260 | +
|
| 261 | + Pair(int first, int second) { |
| 262 | + this.first = first; |
| 263 | + this.second = second; |
| 264 | + } |
| 265 | + } |
| 266 | +} |
| 267 | +
|
| 268 | + ``` |
| 269 | + |
| 270 | + </TabItem> |
| 271 | + <TabItem value="C++" label="C++"> |
| 272 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 273 | + ```cpp |
| 274 | + class Solution { |
| 275 | +public: |
| 276 | + string clearStars(string s) { |
| 277 | + int n = s.size(); |
| 278 | + set<pair<int,int>>st; |
| 279 | + set<int> del; |
| 280 | + for (int i = 0; i < n; i++) { |
| 281 | + if (s[i] == '*') { |
| 282 | + auto first = *st.begin(); |
| 283 | + st.erase(st.begin()); |
| 284 | + del.insert(-first.second); |
| 285 | + del.insert(i); |
| 286 | + } else { |
| 287 | + st.insert({s[i]-'a' , -i}); |
| 288 | + } |
| 289 | + } |
| 290 | + |
| 291 | + string ans = ""; |
| 292 | + for (int i = 0; i < n; i++) if (!del.count(i)) ans += s[i]; |
| 293 | + return ans; |
| 294 | + } |
| 295 | +}; |
| 296 | + ``` |
| 297 | +</TabItem> |
| 298 | +</Tabs> |
| 299 | + |
| 300 | + </TabItem> |
| 301 | +</Tabs> |
| 302 | + |
| 303 | +## References |
| 304 | + |
| 305 | +- **LeetCode Problem**: [ Lexicographically Minimum String After Removing Stars](https://leetcode.com/problems/lexicographically-minimum-string-after-removing-stars/description/) |
| 306 | + |
| 307 | +- **Solution Link**: [LeetCode Solution](https://leetcode.com/problems/lexicographically-minimum-string-after-removing-stars/solutions) |
| 308 | + |
0 commit comments