A 80ms O(n) C++ concise solution
This is a hard question and I don\'t have any idea about it. Then I go to Disucss and see some titles about O(n) and then back to think about it. Then I work ou
This is a hard question and I don\'t have any idea about it. Then I go to Disucss and see some titles about O(n) and then back to think about it. Then I work out the following solution with some wrong tries. I think the previous threads give good solution. Here this code is a bit faster and also code is concise. Thus share to learn with all.\n\nGeneral idea is that we check the number character one by one for both strings, and count each. Count +1 for t, and -1 for s. Then we could say any number in t (max number from 0-9) should be smaller than number in s (min number from 0-9) if they are different.\n\n```\nclass Solution {\npublic:\n    bool isTransformable(string s, string t) {\n\t    if(s.length()!=t.length())return false;\n        int cnt[10] = {0};\n        for(int i = 0;i < s.length();i ++){\n            cnt[t[i]-\'0\'] ++;\n            cnt[s[i]-\'0\'] --;\n            int mp = -1, mn = 10;\n            for(int j = 0;j <= 9;j ++){\n                if(cnt[j] > 0 && j > mp)mp = j;\n                if(cnt[j] < 0 && j < mn)mn = j;\n            }\n            if(mp > mn)return false;\n        }\n        for(int i = 0;i < 10;i ++)if(cnt[i])return false;\n        return true;\n    }\n};\n```
[]