qingcheng奕  

https://oj.leetcode.com/problems/scramble-string/

一个字符串的混排变换,简直太妙了,好题

class Solution {
public:
    bool isScramble(string s1, string s2) {
        if(s1.size() != s2.size())
            return false;
        
        if(s1.size() == 0 || s1 == s2)
            return true;
        
        string sa = s1;
        string sb = s2;
        sort(sa.begin(),sa.end());
        sort(sb.begin(),sb.end());
        if(sa != sb)
            return false;
        
        for(int i = 1; i < s1.size(); i++)
        {
            string s11 = s1.substr(0,i);
            string s12 = s1.substr(i,s1.size() - i);
            
            string s21 = s2.substr(0,i);
            string s22 = s2.substr(i,s2.size() - i);
            string s31 = s2.substr(0,s2.size() - i);
            string s32 = s2.substr(s2.size() - i,s2.size());
            
            if(isScramble(s11,s21)&&isScramble(s12,s22) || isScramble(s11,s32)&&isScramble(s12,s31))
                return true;
        }
        return false;
    }
};

 

posted on 2014-08-16 16:47  qingcheng奕  阅读(125)  评论(0编辑  收藏  举报