Hard-题目17:87. Scramble String

原创 2016年05月31日 23:32:30

题目原文:
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”.
题目大意:
给出Scramble string的定义是,把s1从其中某处拆开,两边组成一棵二叉树的左子树和右子树,再递归进行下去,直到只有一个字符,这样组成了一棵二叉树,其中叶子节点的“轮廓”依次是字符串中的每个字符。如果存在一种二叉树,把其中的一个节点的左右孩子调换,得到的“轮廓”是s2,则称s1和s2是scramble。
给出两个字符串s1和s2,判断s1和s2是不是一对scramble string.
题目分析:
粗看起来很难,其实可以s1和s2是scramble string只有以下几种情况:
(1) s1==s2.
(2) 存在i,使得s1的前i个字符和s2的前i个字符是scramble,后半部分也是scramble。(相当于左子树和右子树不用调换)
(3) 存在i,使得s1的前i个字符和s2的后i个字符是scramble,s1的后半部分和s2的前半部分是scramble。(相当于从第i个点处把s1拆开,再交换左右子树。)
接下来就是递归处理了。需要做一个剪枝处理,就是当s1和s2中的字符不能一一对应时,则一定不是scramble的。
源码:(language:java)

public class Solution {
    public boolean isScramble(String s1, String s2) {
        int len1 = s1.length(),len2 = s2.length();
        if(len1 != len2)
            return false;
        else if(len1==1)
            return s1.charAt(0)==s2.charAt(0);
        else if(s1.equals(s2))
            return true;
        else {
            char[] ca1 = s1.toCharArray(),ca2 = s2.toCharArray();
            Arrays.sort(ca1);
            Arrays.sort(ca2);
            for(int i = 0;i<len1;i++)
                if(ca1[i]!=ca2[i])
                    return false;
            boolean result = false;
            for(int i = 1;i<len1;i++) {
                String s11 = s1.substring(0, i);
                String s12 = s1.substring(i);
                String s21 = s2.substring(0, i);
                String s22 = s2.substring(i);
                String s23 = s2.substring(0,len2-i);
                String s24 = s2.substring(len2-i);
                if(isScramble(s11,s21)&&isScramble(s12,s22) || (isScramble(s11,s24)&&isScramble(s12,s23))) {
                    result = true;
                    break;
                }
            }
            return result;
        }
    }
}

成绩:
7ms,beats 54.91%,众数4ms,21.84%
Cmershen的碎碎念:
这道题还可以用dp来消除递归,我一开始尝试的是记忆化搜索,但没想到Java中有什么合适的数据结构来记录搜索过的字符串。还有这个字符串的切分很适合用python写(比如用[i:],[:i]等很简洁的表示法),但我python写的太少了……语法不太熟练……

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

87. Scramble String

2种思路: 1、递归+剪枝 2、dp思路一、递归判断 S1的左子串L和S2的右子串 && S1的右子串和S2的左子串 是否是Scramble String!! 或者 S1的左子串L...
  • qfzxhy
  • qfzxhy
  • 2016年06月30日 15:23
  • 118

[leetcode]87. Scramble String(Java)

https://leetcode.com/problems/scramble-string/#/description Given a string s1, we may represent...

LeetCode --- 87. Scramble String

题目链接:Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to ...
  • makuiyu
  • makuiyu
  • 2015年04月07日 21:52
  • 1541

leetcode 87 Scramble String(递归+剪枝)

leetcode 87 Scramble String(递归+剪枝)

LeetCode 87 Scramble String

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

[leetcode]87. Scramble String @python

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

LeetCode: Scramble String [87]

【题目】 Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty...

【一天一道LeetCode】#87. Scramble String

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目...

Leetcode:87. Scramble String

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

[leetcode] 87.Scramble String

题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty sub...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Hard-题目17:87. Scramble String
举报原因:
原因补充:

(最多只允许输入30个字)