Short C++ 3ms Solution


  • 1
    B
        string removeDuplicateLetters(string s) {
            vector<int> v(26);
            for(char c:s) v[c-'a']++;
            vector<bool> used(26);
            string res;
            for(int i=0;i<s.length();i++) {
                v[s[i]-'a']--;
                if(used[s[i]-'a']) continue;
                while(!res.empty() && s[i]<res.back() && v[res.back()-'a']) {
                    used[res.back()-'a']=false;
                    res.pop_back();
                }
                res.push_back(s[i]);
                used[s[i]-'a']=true;
            }
            return res;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.