1081.cpp (546B)
1 class Solution { 2 public: 3 string smallestSubsequence(const string &s) { 4 uint16_t last[128] = {0}, seen[128] = {0}; 5 string res = ""; 6 7 for (int i = 0; i < s.size(); i++) 8 last[s[i]] = i; 9 for (int i = 0; i < s.size(); i++) { 10 if (seen[s[i]]++) continue; 11 while (!res.empty() && res.back() > s[i] && i < last[res.back()]) { 12 seen[res.back()] = 0; 13 res.pop_back(); 14 } 15 res.push_back(s[i]); 16 } 17 return res; 18 } 19 };