Python [backward + forward pass with stack]
Very tricky question!\n\n1. Probably the hardest part is to realize that: For every digit in t you find the first occurrence in s. Call that current digit as ke
Very tricky question!\n\n1. Probably the hardest part is to realize that: For every digit in `t` you find the first occurrence in `s`. Call that current digit as `key`. You need to make sure in `s`, no digit smaller than `key` appears to the left.\n2. As we check one-by-one, we need to remove the digits we considered from `s`. In order to do this operation constant time, store all indices in reversed form using `stack` in the first loop (backward pass over `s`). Then use the `.pop()` operation in the second loop (forward pass over `t`).\n\n```\nfrom collections import defaultdict\nclass Solution:\n    def isTransformable(self, s: str, t: str) -> bool:\n        #places: this stores the indices of every digit from 0 to 9\n        places = defaultdict(list)\n        for i in reversed(range(len(s))):\n            key = int(s[i])\n            places[key].append(i)\n        \n        for e in t: #we loop over t and check every digit\n            key = int(e) #current digit\n            if not places[key]: #digit is not in s, return False\n                return False \n            i = places[key][-1] #location of current digit\n            for j in range(key): #only loop over digits smaller than current digit\n                if places[j] and places[j][-1] < i: #there is a digit smaller than current digit, return false\n                    return False\n            places[key].pop()\n        return True\n```
[]