Skip to content

Commit 0f111f3

Browse files
authored
Create lexicographically-minimum-string-after-removing-stars.cpp
1 parent 7055731 commit 0f111f3

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Time: O(n + 26)
2+
// Space: O(n + 26)
3+
4+
// greedy, hash table, stack
5+
class Solution {
6+
public:
7+
string clearStars(string s) {
8+
vector<vector<int>> lookup(26);
9+
for (int i = 0; i < size(s); ++i) {
10+
if (s[i] != '*') {
11+
lookup[s[i] - 'a'].emplace_back(i);
12+
continue;
13+
}
14+
for (auto& stk : lookup) {
15+
if (empty(stk)) {
16+
continue;
17+
}
18+
s[stk.back()] = '*';
19+
stk.pop_back();
20+
break;
21+
}
22+
}
23+
s.erase(remove(begin(s), end(s), '*'), end(s));
24+
return s;
25+
}
26+
};

0 commit comments

Comments
 (0)