Python superclean iterator solution O(1) space
The O(1) space solution suggests we use two pointers to find next reserved chars for S and T from back to front respectively, and compare if the they are identi
The O(1) space solution suggests we use two pointers to find next reserved chars for `S` and `T` from back to front respectively, and compare if the they are identical one pair and another. E.g.\n\n```python\ni, j = len(S) - 1, len(T) - 1\nwhile i >= 0 and j >= 0:\n\twhile i >= 0: # find next reserved char of S\n\t\t...\n\twhile j >= 0: # find next reserved char of T\n\t\t...\n\t\n\tif S[i] == T[j]:\n\t\t...\n```\n\nThis process is actually a repeat work for searching next index of  `S` and `T`, and we can use a helper function to apply the process on both `S` and `T`. \n\nSince the process is essentially yielding reserved chars one by one, in Python, **we can take advantage of generator to create a customized iterator iterating from back through the front, and yield all reserved chars.**\n\nThen we zip the two iterator on S and T, we can compare the chars one pair and another.\n\nThere is a trick that we can yield an empty string at the end indicating the string is iterated over, as such we can identify the cases like `abc` and `bc` where one string is a suffix of the other. Alternatively you can use `itertools.zip_longest` to avoid yielding an empty string at the end of iterating\n\n```python\nclass Solution:\n    def backspaceCompare(self, S, T):\n        """\n        :type S: str\n        :type T: str\n        :rtype: bool\n        """\n        def it(s):\n            cnt = 0\n            for ch in reversed(s):\n                if ch != \'#\' and cnt == 0:\n                    yield ch\n                else:\n                    if ch == \'#\':\n                        cnt += 1\n                    else:\n                        cnt -= 1\n            \n            yield \'\' # indicate the end of iterator\n        \n        return all(s == t for s, t in zip(it(S), it(T)))\n```
['Python']