LeetCode 题解工作台
扰乱字符串
使用下面描述的算法可以扰乱字符串 s 得到字符串 t : 如果字符串的长度为 1 ,算法停止 如果字符串的长度 > 1 ,执行下述步骤: 在一个随机下标处将字符串分割成两个非空的子字符串。即,如果已知字符串 s ,则可以将其分成两个子字符串 x 和 y ,且满足 s = x + y 。 随机 决定是…
2
题型
6
代码语言
3
相关题
当前训练重点
困难 · 状态·转移·动态规划
答案摘要
我们设计一个函数 $dfs(i, j, k)$,表示字符串 从 开始长度为 的子串是否能变换为字符串 从 开始长度为 的子串。如果能变换,返回 `true`,否则返回 `false`。那么答案就是 $dfs(0, 0, n)$,其中 是字符串的长度。 函数 $dfs(i, j, k)$ 的计算方式如下:
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路
题目描述
s 得到字符串 t :
- 如果字符串的长度为 1 ,算法停止
- 如果字符串的长度 > 1 ,执行下述步骤:
- 在一个随机下标处将字符串分割成两个非空的子字符串。即,如果已知字符串
s,则可以将其分成两个子字符串x和y,且满足s = x + y。 - 随机 决定是要「交换两个子字符串」还是要「保持这两个子字符串的顺序不变」。即,在执行这一步骤之后,
s可能是s = x + y或者s = y + x。 - 在
x和y这两个子字符串上继续从步骤 1 开始递归执行此算法。
- 在一个随机下标处将字符串分割成两个非空的子字符串。即,如果已知字符串
给你两个 长度相等 的字符串 s1 和 s2,判断 s2 是否是 s1 的扰乱字符串。如果是,返回 true ;否则,返回 false 。
示例 1:
输入:s1 = "great", s2 = "rgeat" 输出:true 解释:s1 上可能发生的一种情形是: "great" --> "gr/eat" // 在一个随机下标处分割得到两个子字符串 "gr/eat" --> "gr/eat" // 随机决定:「保持这两个子字符串的顺序不变」 "gr/eat" --> "g/r / e/at" // 在子字符串上递归执行此算法。两个子字符串分别在随机下标处进行一轮分割 "g/r / e/at" --> "r/g / e/at" // 随机决定:第一组「交换两个子字符串」,第二组「保持这两个子字符串的顺序不变」 "r/g / e/at" --> "r/g / e/ a/t" // 继续递归执行此算法,将 "at" 分割得到 "a/t" "r/g / e/ a/t" --> "r/g / e/ a/t" // 随机决定:「保持这两个子字符串的顺序不变」 算法终止,结果字符串和 s2 相同,都是 "rgeat" 这是一种能够扰乱 s1 得到 s2 的情形,可以认为 s2 是 s1 的扰乱字符串,返回 true
示例 2:
输入:s1 = "abcde", s2 = "caebd" 输出:false
示例 3:
输入:s1 = "a", s2 = "a" 输出:true
提示:
s1.length == s2.length1 <= s1.length <= 30s1和s2由小写英文字母组成
解题思路
方法一:记忆化搜索
我们设计一个函数 ,表示字符串 从 开始长度为 的子串是否能变换为字符串 从 开始长度为 的子串。如果能变换,返回 true,否则返回 false。那么答案就是 ,其中 是字符串的长度。
函数 的计算方式如下:
- 如果 ,那么只需要判断 和 是否相等,如果相等返回
true,否则返回false; - 如果 ,我们枚举分割部分的长度 ,那么有两种情况:如果不交换分割的两个子字符串,那么就是 ;如果交换了分割的两个子字符串,那么就是 。如果两种情况中有一种情况成立,那么就说明 成立,返回
true,否则返回false。
最后,我们返回 。
为了避免重复计算,我们可以使用记忆化搜索的方式。
时间复杂度 ,空间复杂度 。其中 是字符串的长度。
class Solution:
def isScramble(self, s1: str, s2: str) -> bool:
@cache
def dfs(i: int, j: int, k: int) -> bool:
if k == 1:
return s1[i] == s2[j]
for h in range(1, k):
if dfs(i, j, h) and dfs(i + h, j + h, k - h):
return True
if dfs(i + h, j, k - h) and dfs(i, j + k - h, h):
return True
return False
return dfs(0, 0, len(s1))
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | O(n^4) |
| 空间 | O(n^3) |
面试官常问的追问
外企场景- question_mark
The candidate demonstrates understanding of dynamic programming by explaining the state transition approach and memoization.
- question_mark
The candidate recognizes the importance of efficiently handling recursive calls using memoization to avoid recomputation.
- question_mark
The candidate may struggle with the recursive nature of the problem if they fail to correctly identify the base cases or transitions.
常见陷阱
外企场景- error
The candidate may overlook the importance of memoization, leading to a time complexity of O(n^6) instead of O(n^4).
- error
Failing to account for both swap and non-swap scenarios in recursive calls can result in incorrect answers.
- error
Not properly handling edge cases, such as when the strings are already identical or cannot be scrambled into one another, could lead to unnecessary computation.
进阶变体
外企场景- arrow_right_alt
Increasing the length of the strings beyond the current constraints to test the performance of the algorithm.
- arrow_right_alt
Modifying the problem to check for scrambled substrings within a larger string rather than the whole string.
- arrow_right_alt
Introducing additional characters or larger alphabets in the strings, potentially testing the space and time efficiency of the solution.