[Python] Two stacks with explanation. 60ms with 91% fast
The idea is to use a stack to store alphabets with non-zero counts. In the meantime a dictionary records counts for each alphabet. Every time it will pop one le
The idea is to use a stack to store alphabets with non-zero counts. In the meantime a dictionary records counts for each alphabet. Every time it will pop one letter into output and decrease counts by 1. If this letter has more counts available it will be put into the other stack. Repeat until stack is empty. Running time is ~60ms and 91% faster. \n\n```\nclass Solution:\n    def sortString(self, s: str) -> str:\n        c = collections.Counter(s)\n        stack= []\n        for ch in string.ascii_lowercase[::-1]:\n            if c[ch] > 0:\n                stack.append(ch)\n        res = ""\n        while stack:\n            temp = []\n            while stack:\n                ch = stack.pop()\n                res += ch\n                if c[ch] > 1:\n                    c[ch] -= 1\n                    temp.append(ch)\n            stack = temp[:]\n        return res\n```
[]