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)


源代码文件在这里.


目录
相关文章
|
10月前
|
算法 C++
【LeetCode】【C++】string OJ必刷题
【LeetCode】【C++】string OJ必刷题
56 0
|
5月前
|
机器学习/深度学习 canal NoSQL
从C语言到C++_12(string相关OJ题)(leetcode力扣)
从C语言到C++_12(string相关OJ题)(leetcode力扣)
48 0
|
5月前
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
11月前
|
Java
Leetcode 467. Unique Substrings in Wraparound String
大概翻译下题意,有个无限长的字符串s,是由无数个「abcdefghijklmnopqrstuvwxy」组成的。现在给你一个字符串p,求多少个p的非重复子串在s中出现了?
46 0
|
算法 索引
【LeetCode】string 类的几道简单题
【LeetCode】string 类的几道简单题
【LeetCode】string 类的几道简单题
|
存储 C语言 C++
Leetcode17. 电话号码的字母组合:递归树深度遍历(C++vector和string的小练习)
Leetcode17. 电话号码的字母组合:递归树深度遍历(C++vector和string的小练习)
|
存储 canal 算法
leetcode:43. 字符串相乘(附加一些C++string其他小练习)
leetcode:43. 字符串相乘(附加一些C++string其他小练习)
|
机器学习/深度学习 NoSQL 算法
LeetCode 344. 反转字符串 Reverse String
LeetCode 344. 反转字符串 Reverse String
LeetCode Contest 186 5392. 分割字符串的最大得分 Maximum Score After Splitting a String
LeetCode Contest 186 5392. 分割字符串的最大得分 Maximum Score After Splitting a String
|
28天前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行