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

  • 相关阅读:
    PC远程调试设备(转)
    根据自己的需要,把别人开发好的东西搬过来,优化and重构,在优化的过程中,甚至也会弄出一套全新的东西(转)
    修改Hosts不生效的一个场景-web 专题
    Data URI(转)
    数据仓库与数据挖掘的一些基本概念
    几种常见模式识别算法整理和总结
    史上最简单的Hibernate入门简单介绍
    sprintf,你知道多少?
    一步一步写算法(之洗牌算法)
    ListView的优化
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/13371355.html
Copyright © 2011-2022 走看看