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,s22)

|| F(s11,s22) && F(s12,s21)  i<k<j;

简单的用递归编程会超时, 因为有太多重复的计算, 因此, 我们希望能够减少重复,即每个状态只计算一次, 通常, 这样的状态会用一个矩阵来记录状态。对于这个问题, 一个简单的矩阵不够用, 需要多个矩阵。

先看一些简单的, 是或不是scramble string的状态:

总结出规律: 对角有一组满足,都是true, 就是scramble string。

再看一个3个字母的演化情况:

当维度再一步扩大的时候, 还要综合考虑前面数个表的组合判断,这里我偷懒不画, 原理其实是一样的。

代码地址:

https://github.com/LKF10051/LeetCode/blob/master/LeetCode/scramble_string.cpp

时间: 2024-10-05 18:54:10

leetcode Scramble String 解题思路的相关文章

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] 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.When s3 = "aadbbbaccc", return false. 问题 : 给定三个字符串 s1, s

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

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] 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] Word Break 解题思路

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true because &

[LeetCode] Scramble String -- 三维动态规划的范例

(Version 0.0) 作为一个小弱,这个题目是我第一次碰到三维的动态规划.在自己做的时候意识到了所谓的scramble实际上有两种可能的类型,一类是在较低层的节点进行的两个子节点的对调,这样的情况如果我们从第一层切分点,或者说从较高层的切分点看的话,s1和s2切分点左边的子串所包含的字符的种类个数应该完全一致,同样右边也是完全一致:另一类是在较高层切分点进行的互换,这样我们如果在同层来考察s1和s2的话,会发现s1的切分点左侧的char和s2的切分点右侧的char种类和每种char的数目一

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

[LeetCode] Maximum Gap 解题思路

Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space. Return 0 if the array contains less than 2 elements. You may assume all elements in the array are non-negat