LeetCode: Scramble String [87]

【题目】

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, 我们称s2是s1的scrambled string。

本题输入字符串s1和s2, 判断s2是否是s1的scrambled字符串。

【思路】

1. s1, s2长度必须相等,且拥有相同的元素,且各元素出现的次数也相同。

2. 如果s2是s1的scramble字符串,则s1一定存在一种分裂s11, s12, s2一定存在一种分裂s21,s22, 满足: isScramble(s11, s21)&&isScramble(s12, s22) || isScramble(s11, s22) && isScramble(s12, s21)   其中isScramble(x, y)表示y是x的scramble字符串。

3. 递归判断所有分裂情况。

【代码】

class Solution {
public:
    bool isValid(string s1, string s2){
        //s1和s2应该有相同种的字符,且各字符出现的次数相同
        sort(s1.begin(), s1.end());
        sort(s2.begin(), s2.end());
        if(s1.compare(s2)==0)return true;
        return false;
    }
    bool isScramble(string s1, string s2) {
        if(s1.length()!=s2.length())return false;
        if(s1.length()==0 || s2.length()==0)return false;

        if(s1.compare(s2)==0)return true;       //如果s1和s2相同,则返回true;
        if(s1.length()==1)return false;     //如果s1和s2长度为1, 且不相等,则返回false

        //考虑所有的分裂情况,然后递归判断
        for(int len=1; len<=s1.length()-1; len++){
            string s1left = s1.substr(0, len);
            string s1right = s1.substr(len, s1.length()-len);
            string s2left = s2.substr(0, len);
            string s2right = s2.substr(len, s1.length()-len);
            //在进行下一轮递归是需要先判断输入元素是否合法,如果不合法就没必要进行递归,过多的递归会使时间成本陡增
            if(isValid(s1left, s2left) && isScramble(s1left, s2left) && isScramble(s1right, s2right))return true;
            s2left = s2.substr(0, s1.length()-len);
            s2right = s2.substr(s1.length()-len, len);
            if(isValid(s1left, s2right) && isScramble(s1left, s2right) && isScramble(s1right, s2left))return true;
        }
        return false;
    }
};

LeetCode: Scramble String [87],布布扣,bubuko.com

时间: 2024-10-22 04:48:37

LeetCode: Scramble String [87]的相关文章

LeetCode: Scramble String

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 t

[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 a

Leetcode:Scramble String 解题报告

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,

leetcode Scramble String 解题思路

https://leetcode.com/problems/scramble-string/ 首先递归,或说是递推是最容易想到的: F(s1(i,j), s2(i,j)) = F(s1(i,k),s2(i,k)) && F(s1(j-k,j),s2((j-k,j)) || F(s1(i,k),s2(j-k,j)) && F(s1(j-k,j),s2(i,k))  i<k<j; 图解: F(s1, s2) = F(s11,s21) && F(s12

[leetcode] Scramble String @python

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 a

[LeetCode] Scramble String -- 三维动态规划的范例

(Version 0.0) 作为一个小弱,这个题目是我第一次碰到三维的动态规划.在自己做的时候意识到了所谓的scramble实际上有两种可能的类型,一类是在较低层的节点进行的两个子节点的对调,这样的情况如果我们从第一层切分点,或者说从较高层的切分点看的话,s1和s2切分点左边的子串所包含的字符的种类个数应该完全一致,同样右边也是完全一致:另一类是在较高层切分点进行的互换,这样我们如果在同层来考察s1和s2的话,会发现s1的切分点左侧的char和s2的切分点右侧的char种类和每种char的数目一

[LeetCode] Scramble String 字符串 dp

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 a

[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 a

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

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