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

字符串的好题。题干解释的非常复杂,一下让人不知所措了。

这道题到底是什么意思呢?最终的结果是把一个字符串中字母的顺序打乱了,让你判断一个字符串能不能由另一个字符串打乱得到。那打乱这个过程是怎么做的呢,很简单,给你一个字符串,你必须先找一个点把它砍成两半,你可以通过交换这两半的顺序来打乱源字符串的顺序,也就是在两半中的字符与另一半中所有字符的相对顺序是统一的。对于每一半,都可以重复上面的过程。

那想一下,怎么知道打断的那个点在哪呢?穷举。怎么知道打断之后有没有做交换操作呢?两种情况递归,有一条走的通就可以了。还有个问题,两个字符串中包含的字符一定是完全一样的,怎样确定这一点呢?最暴力的方式,新开两个字符串,排序,判断这两个新的相不相等。

比如: "abcd", "bdac" are not scramble string

代码:

class Solution:
    # @return a boolean
    def isScramble(self, s1, s2):
        if s1==s2: return True
        if sorted(s1) != sorted(s2): return False  # "abcd", "bdac" are not scramble string
        length=len(s1)
        for i in range(1,length):
            if self.isScramble(s1[:i],s2[:i])        and self.isScramble(s1[i:],s2[i:]):        return True
            if self.isScramble(s1[:i],s2[length-i:]) and self.isScramble(s1[i:],s2[:length-i]): return True
        return False
时间: 2024-11-01 07:21:21

[leetcode] Scramble String @python的相关文章

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]Interleaving String @ Python

原题地址:https://oj.leetcode.com/problems/interleaving-string/ 题意: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = "aabcc",s2 = "dbbca", When s3 = "aadbbcbcac", return true.Whe

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

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 -- 三维动态规划的范例

(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