C++ O(n), identical to #316
The solution is exactly the same as for 316. Remove Duplicate Letters.\n\nFrom the input string, we are trying to greedily build a monotonically increasing resu
The solution is exactly the same as for [316. Remove Duplicate Letters](https://leetcode.com/problems/remove-duplicate-letters).\n\nFrom the input string, we are trying to greedily build a monotonically increasing result string. If the next character is smaller than the back of the result string, we remove larger characters from the back **providing** there are more occurrences of that character **later** in the input string.\n```\nstring smallestSubsequence(string s, string res = "") {\n  int cnt[26] = {}, used[26] = {};\n  for (auto ch : s) ++cnt[ch - \'a\'];\n  for (auto ch : s) {\n    --cnt[ch - \'a\'];\n    if (used[ch - \'a\']++ > 0) continue;\n    while (!res.empty() && res.back() > ch && cnt[res.back() - \'a\'] > 0) {\n      used[res.back() - \'a\'] = 0;\n      res.pop_back();\n    }\n    res.push_back(ch);\n  }\n  return res;\n}   \n```\n## Complexity Analysis\nRuntime: O(n). We process each input character no more than 2 times.\nMemory: O(1). We need the constant memory to track up to 26 unique letters.
[]