代码拉取完成,页面将自动刷新
// Time: O(n)
// Space: O(k), k is size of the alphabet
// vector solution, need to know size of the alphabet in advance (4ms)
class Solution {
public:
string removeDuplicateLetters(string s) {
const int k = 26;
vector<int> remaining(k);
for (const auto& c : s) {
++remaining[c - 'a'];
}
vector<bool> in_stack(k);
string stk;
for (const auto& c : s) {
if (!in_stack[c - 'a']) {
while (!stk.empty() && stk.back() > c && remaining[stk.back() - 'a']) {
in_stack[stk.back() - 'a'] = false;
stk.pop_back();
}
stk.push_back(c);
in_stack[c - 'a'] = true;
}
--remaining[c - 'a'];
}
return stk;
}
};
// Time: O(n)
// Space: O(k), k is size of the alphabet
// hash solution, no need to know size of the alphabet in advance (16ms)
class Solution2 {
public:
string removeDuplicateLetters(string s) {
unordered_map<char, int> remaining;
for (const auto& c : s) {
++remaining[c];
}
unordered_set<char> in_stack;
string stk;
for (const auto& c : s) {
if (!in_stack.count(c)) {
while (!stk.empty() && stk.back() > c && remaining[stk.back()]) {
in_stack.erase(stk.back());
stk.pop_back();
}
stk.push_back(c);
in_stack.emplace(c);
}
--remaining[c];
}
return stk;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。