Skip to content

Commit 01cde2c

Browse files
committed
Time: 50 ms (85.71%), Space: 24.7 MB (85.06%) - LeetHub
1 parent 87e845f commit 01cde2c

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

3170-lexicographically-minimum-string-after-removing-stars/3170-lexicographically-minimum-string-after-removing-stars.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
class Solution {
22
public:
33
string clearStars(string s) {
4-
map<int, vector<int>> mp;
4+
vector<vector<int>> f(26);
55
int n = s.size();
6-
vector<bool> notTake(n);
6+
vector<bool> removed(n);
77
for (int i = 0; i < n; i++) {
88
if (s[i] == '*') {
9-
notTake[i] = true;
10-
notTake[mp.begin()->second.back()] = true;
11-
mp.begin()->second.pop_back();
12-
if (mp.begin()->second.size() == 0) {
13-
mp.erase(mp.begin()->first);
9+
removed[i] = true;
10+
for (int j = 0; j < 26; j++) {
11+
if (!f[j].empty()) {
12+
removed[f[j].back()] = true;
13+
f[j].pop_back();
14+
break;
15+
}
1416
}
1517
} else {
16-
mp[s[i]].push_back(i);
18+
f[s[i] - 'a'].push_back(i);
1719
}
1820
}
1921
string ans;
2022
for (int i = 0; i < n; i++) {
21-
if (notTake[i]) continue;
23+
if (removed[i]) continue;
2224
ans += s[i];
2325
}
2426
return ans;

0 commit comments

Comments
 (0)