show my thinking process
consider s = \'ecbacaba\', ans = \'eacb\'\nwhy the first letter ans is not \'a\'? since there are only one \'e\' and its index is smaller than the first \'a\'\n
consider s = \'ecbacaba\', ans = \'eacb\'\nwhy the first letter ans is not \'a\'? since there are only one \'e\' and its index is smaller than the first \'a\'\nNow we find the key point!\n\nfor example, s = \'cdadabcc\', `s` contains `a, b, c, d`\nindex[\'a\'] = [2, 4]\nindex[\'b\'] = [5]\nindex[\'c\'] = [0, 6, 7]\nindex[\'d\'] = [1, 3]\n\nwhich will be the first letter in ans?\nit will be letter \'a\', cause `2 < min(5, 7, 3)`, (5,7,3) is the `last index` of other letters\n\nafter append \'a\', now the index becomes\nindex[\'b\'] = [5]\nindex[\'c\'] = [6, 7]\nindex[\'d\'] = [3]\n`we delete all index less than 2`\nobviously, the next letter will be \'d\'.\nanalyse one by one.\n\n```python\nclass Solution:\n    def smallestSubsequence(self, text: str) -> str:\n        n = len(text)\n        d = collections.defaultdict(collections.deque)\n        # record index\n        for i,v in enumerate(text):\n            d[ord(v)-ord(\'a\')].append(i)\n        \n        # search orderly\n        keys = sorted(d.keys())\n        res = []\n        c = len(d)\n        last_index = -1\n        while len(res) < c: # len(res) will not larger than 26\n            # O(26*26) time, search the first smallest letter\n            for i in range(len(keys)):\n                if all(d[keys[i]][0] < d[keys[j]][-1] for j in range(i+1, len(keys))):\n                    res.append(chr(ord(\'a\')+keys[i]))\n                    last_index = d[keys[i]][0]\n                    keys.remove(keys[i])\n                    break\n            # O(n) time, delete all index less than last_index\n            for i in range(len(keys)):\n                while d[keys[i]] and d[keys[i]][0] < last_index:\n                    d[keys[i]].popleft()\n  \n        return \'\'.join(res)\n```
[]