Python 3 || Two-pointer, O(n) / O(1), 95% / 100%
Intuition\n Describe your first thoughts on how to solve this problem. \nCheck if there is enough amount of valid digits to allow the swap of the current digit.
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nCheck if there is enough amount of valid digits to allow the swap of the current digit.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTake `s = "14532", t = "12345"` as an exmaple.\n\nFor `sptr = 4, tptr = 1, s[sptr] == t[tptr] == \'2\'`, if we want to move \'2\' from position 4 to position 1, we need to make sure that there are at least `4-1 == 3` digits in `s[:sptr]` that are larger than \'2\', so that \'2\' can reach position 1 by swapping with these digits afterward. \n\nThis is checked by `sum(counter[d:]) >= sptr-tptr` in the code.\n\nSimilarly, when `sptr < tptr`, we check it reversely in the second pass. For `sptr = 1, tptr = 3, s[sptr] == t[tptr] == \'4\'`, there should be at least `3-1 == 2` digits in `s[sptr+1:]` that are smaller than \'4\', which is `sum(counter[:d]) >= tptr-sptr`. \n\n\n# Complexity\n- Time complexity: $$O(dn)$$, $$d = 10$$ is the number of characters.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(d)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n    def isTransformable(self, s: str, t: str) -> bool:\n        counter, n, sptr = [0]*10, len(s), 0\n        for tptr in range(n):\n            d = ord(t[tptr])-48\n            if counter[d]:\n                counter[d] -= 1\n            else:\n                while sptr < n and s[sptr] != t[tptr]:\n                    counter[ord(s[sptr])-48] += 1\n                    sptr += 1\n                if sptr == n or sum(counter[d:]) < sptr-tptr:\n                    return False\n                sptr += 1\n  \n        sptr -= 1\n        for tptr in range(n-1, -1, -1):\n            d = ord(t[tptr])-48\n            if counter[d]:\n                counter[d] -= 1\n            else:\n                while sptr >= 0 and s[sptr] != t[tptr]:\n                    counter[ord(s[sptr])-48] += 1\n                    sptr -= 1\n                if sptr == -1 or sum(counter[:d]) < tptr-sptr:\n                    return False\n                sptr -= 1\n                \n        return True\n```
['Greedy', 'Counting', 'Python', 'Python3']