87. Scramble String

class Solution {
    public boolean isScramble(String s1, String s2) {
        if(s1.length()==0||s1.equals(s2))
            return true;
        int[] cnt=new int[128];
        for(int i=0;i<s1.length();i++)
        {
            cnt[s1.charAt(i)]++;
            cnt[s2.charAt(i)]--;
        }
        for(int i=0;i<128;i++)
            if(cnt[i]!=0)
                return false;
        for(int i=1;i<s1.length();i++)
        {
            if(isScramble(s1.substring(0,i), s2.substring(0,i))&&isScramble(s1.substring(i),s2.substring(i)))
                return true;
            if(isScramble(s1.substring(0,i), s2.substring(s1.length()-i))&&isScramble(s1.substring(i), s2.substring(0, s1.length()-i)))
                return true;
        }
        return false;
    }
}

 

posted @ 2017-09-28 06:56  Weiyu Wang  阅读(105)  评论(0编辑  收藏  举报