Posted on

Description

Submission

class Solution {
public:
    bool isTransformable(string s, string t) {
        vector<queue<int>> idx(10);
        
        for(int i = 0; i < s.size(); ++i) {
            idx[s[i]-'0'].push(i);
        }
        
        for(int i = 0; i < t.size(); ++i) {
            int num = t[i] - '0';
            if(idx[num].empty()) return false;
            int pos = idx[num].front();
            
            for(int j = 0; j < num; ++j) {
                if(!idx[j].empty() && idx[j].front() < pos) 
                    return false;
            }
            idx[num].pop();
        }
        return true;
    }
};

References

  1. https://www.youtube.com/watch?v=Rmr1Pn6ws2c

Leave a Reply

Your email address will not be published. Required fields are marked *