LeetCode第 1081 题:不同字符的最小子序列(C++)

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;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_32523711/article/details/107891704