Scramble String

来源:互联网 发布:通风管道设计绘制软件 编辑:程序博客网 时间:2024/06/05 21:13

题目:

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.

Below is one possible representation of s1 = "great":

    great   /    \  gr    eat / \    /  \g   r  e   at           / \          a   t

To scramble the string, we may choose any non-leaf node and swap its two children.

For example, if we choose the node "gr" and swap its two children, it produces a scrambled string "rgeat".

    rgeat   /    \  rg    eat / \    /  \r   g  e   at           / \          a   t

We say that "rgeat" is a scrambled string of "great".

Similarly, if we continue to swap the children of nodes "eat" and "at", it produces a scrambled string "rgtae".

    rgtae   /    \  rg    tae / \    /  \r   g  ta  e       / \      t   a

We say that "rgtae" is a scrambled string of "great".

Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.

自己的思路竟然对了,自己做这题的时候感觉自己的想法肯定会超时的,没想到通过了,不过还是有些地方可以优化的,代码不够简洁。其实想法就是对S1,S2各自可以找到一个分割点,使得S11与S21,S12与S22组成元素相等,或者S11与S22,S12与S21组成元素相等,并依此递归,若都满足则是,否则则不是,要注意,分割可能会存在好几种情况,每一种都要尝试。

自己的代码:比较low,请勿吐槽..

class Solution {
public:
    bool isScramble(string s1, string s2) {
        int size=s1.size();
        if(size==0)
            return false;
        if(size==1){
            if(s1==s2)
                return true;
            else
                return false;
        }
        if(s1==s2)
            return true;
        return check(s1,s2);
    }
    bool check(string s1,string s2){
        if(s1.size()==1){
            if(s1==s2)
                return true;
            else
                return false;
        }
        if(s1==s2)
            return true;
        int pos=-1,dir=1;
        string temp1,temp2,temp3,temp4;
        bool result=false;
        for(int i=0;i<s1.size()-1;++i){
            temp1=string(s1.begin(),s1.begin()+i+1);temp2=string(s2.begin(),s2.begin()+i+1);
            temp3=string(s1.begin()+i+1,s1.end());temp4=string(s2.begin()+i+1,s2.end());
            sort(temp1.begin(),temp1.end());
            sort(temp2.begin(),temp2.end());
            sort(temp3.begin(),temp3.end());
            sort(temp4.begin(),temp4.end());
            if(temp1==temp2 && temp3==temp4){
                pos=i;
                dir=1;
                temp1=string(s1.begin(),s1.begin()+pos+1);temp2=string(s2.begin(),s2.begin()+pos+1);
                temp3=string(s1.begin()+pos+1,s1.end());temp4=string(s2.begin()+pos+1,s2.end());
                result=check(temp1,temp2) && check(temp3,temp4);
                if(result==true)
                    return true;
                
            }
            temp1=string(s1.begin(),s1.begin()+i+1);temp2=string(s2.end()-i-1,s2.end());
            temp3=string(s1.begin()+i+1,s1.end());temp4=string(s2.begin(),s2.end()-i-1);
            sort(temp1.begin(),temp1.end());
            sort(temp2.begin(),temp2.end());
            sort(temp3.begin(),temp3.end());
            sort(temp4.begin(),temp4.end());
            if(temp1==temp2 && temp3==temp4){
                pos=i;
                dir=0;
                temp1=string(s1.begin(),s1.begin()+pos+1);temp2=string(s2.end()-pos-1,s2.end());
                temp3=string(s1.begin()+pos+1,s1.end());temp4=string(s2.begin(),s2.end()-pos-1);  
                result=check(temp1,temp2) && check(temp3,temp4);
                if(result==true)
                    return true;
            }
        }
        return false;
    }
};


网上看到的比较简洁的代码:

  1. bool isScramble(string s1, string s2) {  
  2.         // Note: The Solution object is instantiated only once.  
  3.         if(s1.length() != s2.length()) return false;  
  4.         if(s1 == s2) return true;  
  5.   
  6.         int A[26] = {0};  
  7.         for(int i = 0; i < s1.length(); i++)  
  8.             ++A[s1[i]-'a'];  
  9.   
  10.         for(int j = 0; j < s2.length(); j++)  
  11.             --A[s2[j]-'a'];  
  12.   
  13.         for(int k = 0; k < 26; k++)  
  14.             if(A[k] != 0) return false;  
  15.   
  16.         for(int i = 1; i < s1.length(); i++)  
  17.         {  
  18.             bool result = isScramble(s1.substr(0,i), s2.substr(0,i))   
  19.                 && isScramble(s1.substr(i), s2.substr(i));  
  20.             result = result || (isScramble(s1.substr(0,i), s2.substr(s2.length()-i, i))  
  21.                 && isScramble(s1.substr(i), s2.substr(0,s2.length()-i)));  
  22.             if(result) return true;  
  23.         }  
  24.         return false;  
  25.     }  

整体思路是一样的,但是网上这个明显更优。

,
0 0