鱼C论坛

 找回密码
 立即注册
查看: 1812|回复: 0

[学习笔记] Leetcode 87. Scramble String

[复制链接]
发表于 2020-9-24 11:15:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
We can scramble a string s to get a string t using the following algorithm:

If the length of the string is 1, stop.
If the length of the string is > 1, do the following:
Split the string into two non-empty substrings at a random index, i.e., if the string is s, divide it to x and y where s = x + y.
Randomly decide to swap the two substrings or to keep them in the same order. i.e., after this step, s may become s = x + y or s = y + x.
Apply step 1 recursively on each of the two substrings x and y.
Given two strings s1 and s2 of the same length, return true if s2 is a scrambled string of s1, otherwise, return false.



Example 1:

Input: s1 = "great", s2 = "rgeat"
Output: true
Explanation: One possible scenario applied on s1 is:
"great" --> "gr/eat" // divide at random index.
"gr/eat" --> "gr/eat" // random decision is not to swap the two substrings and keep them in order.
"gr/eat" --> "g/r / e/at" // apply the same algorithm recursively on both substrings. divide at ranom index each of them.
"g/r / e/at" --> "r/g / e/at" // random decision was to swap the first substring and to keep the second substring in the same order.
"r/g / e/at" --> "r/g / e/ a/t" // again apply the algorithm recursively, divide "at" to "a/t".
"r/g / e/ a/t" --> "r/g / e/ a/t" // random decision is to keep both substrings in the same order.
The algorithm stops now and the result string is "rgeat" which is s2.
As there is one possible scenario that led s1 to be scrambled to s2, we return true.
Example 2:

Input: s1 = "abcde", s2 = "caebd"
Output: false
Example 3:

Input: s1 = "a", s2 = "a"
Output: true


Constraints:

s1.length == s2.length
1 <= s1.length <= 30
s1 and s2 consist of lower-case English letters.

  1. class Solution:
  2.     def isScramble(self, s1: str, s2: str) -> bool:
  3.         n = len(s1)
  4.         dp = [[[False for _ in range(n + 1)] for _ in range(n)] for _ in range(n)]
  5.         
  6.         for i in range(n):
  7.             for j in range(n):
  8.                 dp[i][j][1] = s1[i] == s2[j]
  9.         
  10.         for length in range(2, n + 1):
  11.             for i in range(n - length + 1):
  12.                 for j in range(n - length + 1):
  13.                     dp[i][j][length] = False
  14.                     
  15.                     for w in range(1, length):
  16.                         if dp[i][j][w] and dp[i + w][j + w][length - w]:
  17.                             dp[i][j][length] = True
  18.                             break
  19.                     
  20.                     for w in range(1, length):
  21.                         if dp[i][j + length - w][w] and dp[i + w][j][length - w]:
  22.                             dp[i][j][length] = True
  23.                             break
  24.         
  25.         return dp[0][0][n]
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-24 16:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表