C++, O(n) time complexity and O(1) memory
\nclass Solution {\npublic:\n    string smallestSubsequence(string str) {\n        string res;\n        unordered_map<char, int> m;\n        \n        for (auto
```\nclass Solution {\npublic:\n    string smallestSubsequence(string str) {\n        string res;\n        unordered_map<char, int> m;\n        \n        for (auto c: str) m[c]++;\n        if (str.size() == m.size()) return str;\n        \n        for (int i = 0; i < str.size(); i++) {\n            while (res.size() > 0 && res.back() > str[i] && m[res.back()] > 0 && (res.find(str[i]) == string::npos)) {\n                res.pop_back();\n            }\n            if (res.find(str[i]) == string::npos) {\n                res.push_back(str[i]);\n            }\n            m[str[i]]--;\n        }\n        return res;\n    }\n};\n```
[]