zoukankan      html  css  js  c++  java
  • LeetCode-Scramble String

    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.

    class Solution {
    public:
        bool isS(string& s1,string& s2,int begin1,int begin2,int end1,int end2)
        {
            int length1=end1-begin1+1;
            int length2=end2-begin2+1;
            
           
            if(length1!=length2)return false;
            
            if(length1==1){
                if(s1[begin1]==s2[begin2])return true;
                else return false;
            }
            else{
                vector<int> count;
                count.resize(26);
                for(int i=0;i<26;i++)count[i]=0;
                for(int i=begin1;i<=end1;i++){
                    count[s1[i]-'a']++;
                }
                for(int i=begin2;i<=end2;i++){
                    count[s2[i]-'a']--;
                }
                for(int i=0;i<26;i++){
                    if(count[i]!=0){
                        return false;
                    }
                }
                for(int i=1;i<length1;i++){
                    bool b1,b2,b3,b4;
                    int j=length1-i;
                    b1=isS(s1,s2,begin1,begin2,begin1+i-1,begin2+i-1);
                    b2=isS(s1,s2,begin1+i,begin2+i,end1,end2);
                    b3= isS(s1,s2,begin1,end2-i+1,begin1+i-1,end2);
                    b4=isS(s1,s2,begin1+i,begin2,end1,begin2+j-1);
                    if(
                        b1&&b2
                    ||
                        b3&&b4
                    )
                    return true;
                }
                return false;
            }
        }
        bool isScramble(string s1, string s2) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            return isS(s1,s2,0,0,s1.length()-1,s2.length()-1);
        }
    };
  • 相关阅读:
    Swing-文本输入组件(一)
    JFrame常用属性设置模板
    Swing-BoxLayout用法-入门
    linux创建和查看用户命令
    elasticsearch,http://ip:9200访问不到的解决办法
    U盘制作Linux系统盘
    Linux中oracle的安装,亲测
    Linux中yum的安装
    Linux中oracle安装时候报ora-00119解决办法
    eclipse中多个工程编译到同一个目录下
  • 原文地址:https://www.cnblogs.com/superzrx/p/3333325.html
Copyright © 2011-2022 走看看