LeetCode 87. Scramble String

简介: 题意是给定两个字符串,判断他们是否满足某种关系.

v2-42d4e571a40198f49cfac3ebea23b072_1440w.jpg

Description



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


描述



  • 题意是给定两个字符串,判断他们是否满足某种关系.


思路



  • 根据题意,我们观察二叉树的叶子节点,有如下关系:
  • (左子树 == 左子树 and 右子树 == 右子树) or (左子树 == 右子树 and 右子树 == 左子树).
  • 我们以上图第一图和第二图为例:
  • 第一图左串gr == 第二图左串 rg,第一图右串eat == 第二图右串eat.
  • 其中gr == rg:第一图gr的左串g == 第二图rg右串g,第一图gr的右串r == 第二图rg左串r,
  • 其中eat == eat:第一图左串e = 第二图左串 e;第一图右串at == 第二图右串at:第一图左串 a = 第二图左串a,第一图右串= 第二图右串


# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2018-12-25 15:11:08
# @Last Modified by:   何睿
# @Last Modified time: 2018-12-25 16:25:11
class Solution:
    def isScramble(self, s1, s2):
        """
        :type s1: str
        :type s2: str
        :rtype: bool
        """
        # 如果有一个字符串为空,或者两个字符串的长度不相等,则返回False
        if not s1 or not s2 or len(s1) != len(s2):
            return False
        # 如果字符串长度为1,并且字符相等则返回True
        elif len(s1) == 1 and s1 == s2:
            return True
        # 如果两个字符含有的字符不同,返回False
        elif sorted(s1) != sorted(s2):
            return False
        length = len(s1)
        for i in range(1, length):
            left, right = s1[0:i], s1[i:length]
            # 每一个位置都被分开
            # 如果 左==左 and 右 == 右 或者 左==右 and 右== 左则返回Ture
            if (self.isScramble(left, s2[0:i]) and self.isScramble(right, s2[i:len(s2)])) or \
                    (self.isScramble(left, s2[len(s2) - i:len(s2)]) and self.isScramble(right, s2[0:len(s2)-i])):
                return True
        # 否则返回False
        return False
if __name__ == "__main__":
    so = Solution()
    res = so.isScramble("abcdefghijklmnopq", 'efghijklmnopqcadb')
    print(res)


源代码文件在这里.


目录
相关文章
|
存储 canal 算法
leetcode:43. 字符串相乘(附加一些C++string其他小练习)
leetcode:43. 字符串相乘(附加一些C++string其他小练习)
|
算法 索引
【LeetCode】string 类的几道简单题
【LeetCode】string 类的几道简单题
【LeetCode】string 类的几道简单题
|
机器学习/深度学习 NoSQL 算法
LeetCode 344. 反转字符串 Reverse String
LeetCode 344. 反转字符串 Reverse String
LeetCode 438. Find All Anagrams in a String
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. The order of output does not matter.
264 0
LeetCode 438. Find All Anagrams in a String
|
存储
LeetCode 394. Decode String
给定一个经过编码的字符串,返回它解码后的字符串。 编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次。注意 k 保证为正整数。 你可以认为输入字符串总是有效的;输入字符串中没有额外的空格,且输入的方括号总是符合格式要求的。 此外,你可以认为原始数据不包含数字,所有的数字只表示重复的次数 k ,例如不会出现像 3a 或 2[4] 的输入。
307 0
LeetCode 394. Decode String
|
索引
LeetCode 345. Reverse Vowels of a String
编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
250 0
LeetCode 345. Reverse Vowels of a String
|
机器学习/深度学习 NoSQL
LeetCode 344. Reverse String
编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。 不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。
247 0
LeetCode 344. Reverse String
|
索引
LeetCode 97. Interleaving String
给定s1,s2,s3,确定s3是否由s1和s2的交织形成,若是返回True,若不是则返回False.
264 0
LeetCode 97. Interleaving String
Leetcode-Easy 806. Number of Lines To Write String
Leetcode-Easy 806. Number of Lines To Write String
205 0
LeetCode之First Unique Character in a String
LeetCode之First Unique Character in a String
260 0