3170: Lexicographically-Minimum-String-After-Removing-Stars
Medium
table of contents
My initial solution was basically, we can sort all the letters
in a priority queue in the order at which they should
be getting removed from s
if an asterisk
was to appear.
Then, keeping like a bitmap of all the letters that
still remain after one pass of the string, we can construct what the
resulting string should look like after removing all
*
characters.
code
class Solution {
public:
(string s) {
string clearStarsauto comparator = [&](auto first, auto second) {
if (first.first != second.first) {
return first.first > second.first;
} else {
return first.second < second.second;
}
};
<bool> bitmap (s.size(), true);
vector<pair<char, int>, vector<pair<char, int>>, decltype(comparator)> pq(comparator);
priority_queuefor (int i = 0; i < s.size(); ++i) {
if (s[i] != '*') {
.push({s[i], i});
pq} else {
[i] = false;
bitmap[pq.top().second] = false;
bitmap.pop();
pq}
}
= "";
string ans for (int i = 0; i < s.size(); ++i) {
if (bitmap[i]) {
+= s[i];
ans }
}
return ans;
}
};
complexity
learnings
The other solution that I saw revolved around keeping 26 stacks (for each letter of the alphabet) and only popping from the smallest stack.
NOTE: wait… I do not need another bitmap array… I
can just swap out removed letters in s
with an
asterisk…
class Solution {
public:
(string s) {
string clearStars<stack<int>> lastLetter(26);
vector
for (int i = 0; i < s.size(); ++i) {
if (s[i] != '*') {
[s[i]-'a'].push(i);
lastLetter} else {
for (int i = 0; i < 26; ++i) {
if (!lastLetter[i].empty()) {
[lastLetter[i].top()] = '*';
s[i].pop();
lastLetterbreak;
}
}
}
}
= "";
string ans for (int i = 0; i < s.size(); ++i) {
if (s[i] != '*') {
+= s[i];
ans }
}
return ans;
}
};
and an improved version of the original solution above (removing the
bitmap
array and just replacing the deleted letters
in-place with an asterisk *
):
class Solution {
public:
(string s) {
string clearStarsauto comparator = [&](auto first, auto second) {
if (first.first != second.first) {
return first.first > second.first;
} else {
return first.second < second.second;
}
};
<pair<char, int>, vector<pair<char, int>>, decltype(comparator)> pq(comparator);
priority_queuefor (int i = 0; i < s.size(); ++i) {
if (s[i] != '*') {
.push({s[i], i});
pq} else {
[pq.top().second] = '*';
s.pop();
pq}
}
= "";
string ans for (int i = 0; i < s.size(); ++i) {
if (s[i] != '*') {
+= s[i];
ans }
}
return ans;
}
};