zoukankan      html  css  js  c++  java
  • 87. 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.

    Example 1:

    Input: s1 = "great", s2 = "rgeat"
    Output: true
    

    Example 2:

    Input: s1 = "abcde", s2 = "caebd"
    Output: false
    class Solution {
        public boolean isScramble(String s1, String s2) {
            if(s1.equals(s2)) return true;
            if(s1.length() != s2.length()) return false;
            int[] arr = new int[26];
            
            for(int i = 0; i < s1.length(); i++) {
                arr[s1.charAt(i) - 'a']++;
                arr[s2.charAt(i) - 'a']--;
            }
            for(int i = 0; i < 26; i++) if(arr[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;
        }
    }

    总结:

    We use recursive way to solve this question. If s1 == s2, return true obviously, then we check if s1's chars are s2's chars, if not return false;

    Then we know the rotate pivot can happen anywhere from 1 to s1.length() - 1,so for each rotate(scramble) pivot, we tried in two situations:

    1. we didn't rotate at this point so we compare the scrambled strings by simply divide them to two parts

    2. we scrambled at this point so we compare s1's first part with s2's second part and s2's first part with s1's second part like the image

     https://www.youtube.com/watch?v=sETxfdHwxc0

  • 相关阅读:
    html5画布
    html5标签及语义化
    MarkDown:基础知识
    20180624_Git分支管理之多人协作
    20180624_Git管理之Feature分支
    20180619_Git 分支管理之bug分支
    20180618_Git分支管理策略, 不使用Fast forward模式.
    20180618_Git分支管里之解决冲突
    20180618_Git的分支管理
    20180618_在githhub上创建远程仓库,再克隆远程仓库的代码
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/13371355.html
Copyright © 2011-2022 走看看