1081. 不同字符的最小子序列 - 力扣(LeetCode)
和LeetCode第 316 题:去除重复字母(C++)_qq_32523711的博客-CSDN博客相同
class Solution {
public:
string res;
string smallestSubsequence(string text) {
int count[26] = {0};
int contained[26] = {0};
for(const auto &c : text) ++count[c - 'a'];
for(const auto &c : text){
--count[c - 'a'];
if(contained[c - 'a']) continue;
while(!res.empty() && c < res.back() && count[res.back() - 'a']){
contained[res.back() - 'a'] = 0;
res.pop_back();
}
res += c;
contained[c - 'a'] = 1;
}
return res;
}
};