LeetCode之“动态规划”:Interleaving String

  题目链接

  题目要求: 

  Given s1s2s3, 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.

  这道题的难度还是很大。

  在GeeksforGeeks上举了一个例子来说明什么是Interleaving String:

Input: str1 = "AB",  str2 = "CD"
Output:
    ABCD
    ACBD
    ACDB
    CABD
    CADB
    CDAB

Input: str1 = "AB",  str2 = "C"
Output:
    ABC
    ACB
    CAB

  具体的对该题的分析和解决参考了一博文

  像这种判断能否按照某种规则来完成求是否或者某个量的题目,很容易会想到用动态规划来实现。
  先说说维护量,res[i][j]表示用s1的前i个字符和s2的前j个字符能不能按照规则表示出s3的前i+j个字符,如此最后结果就是res[s1.length()][s2.length()],判断是否为真即可。接下来就是递推式了,假设知道res[i][j]之前的所有历史信息,我们怎么得到res[i][j]。可以看出,其实只有两种方式来递推,一种是选取s1的字符作为s3新加进来的字符,另一种是选s2的字符作为新进字符。而要看看能不能选取,就是判断s1(s2)的第i(j)个字符是否与s3的i+j个字符相等。如果可以选取并且对应的res[i-1][j](res[i][j-1])也为真,就说明s3的i+j个字符可以被表示。这两种情况只要有一种成立,就说明res[i][j]为真,是一个或的关系。所以递推式可以表示成

res[i][j] = res[i-1][j]&&s1.charAt(i-1)==s3.charAt(i+j-1) || res[i][j-1]&&s2.charAt(j-1)==s3.charAt(i+j-1)

  时间上因为是一个二维动态规划,所以复杂度是O(m*n),m和n分别是s1和s2的长度。最后就是空间花费,可以看出递推式中只需要用到上一行的信息,所以我们只需要一个一维数组就可以完成历史信息的维护,为了更加优化,我们把短的字符串放在内层循环,这样就可以只需要短字符串的长度即可,所以复杂度是O(min(m,n))。

  具体程序如下:

 1 class Solution {
 2 public:
 3     bool isInterleave(string s1, string s2, string s3) {
 4         int szS1 = s1.size();
 5         int szS2 = s2.size();
 6         int szS3 = s3.size();
 7         if(szS3 != szS1 + szS2)
 8             return false;
 9         if(szS1 == 0 && szS2 == 0)
10             return s3 == s1 || s3 == s2;
11
12         string minWord = szS1 > szS2 ? s2 : s1;
13         string maxWord = szS1 > szS2 ? s1 : s2;
14         int szMinWord = min(szS1, szS2);
15         int szMaxWord = max(szS1, szS2);
16
17         vector<bool> match(szMinWord + 1, true);
18         for(int i = 1; i < szMinWord + 1; i++)
19         {
20             match[i] = match[i - 1] && minWord[i - 1] == s3[i - 1];
21         }
22
23         for(int i = 0; i < szMaxWord; i++)
24         {
25             match[0] = match[0] && s3[i] == maxWord[i];
26             for(int j = 1; j < szMinWord + 1; j++)
27             {
28                 match[j] = (match[j - 1] && minWord[j - 1] == s3[i + j]) || (match[j] && maxWord[i] == s3[i + j]);
29             }
30         }
31
32         return match[szMinWord];
33     }
34 };
时间: 2024-11-07 19:02:29

LeetCode之“动态规划”:Interleaving String的相关文章

leetcode || 97、Interleaving String

problem: 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. Hide

[LeetCode]Interleaving 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.W

LeetCode: Interleaving 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"

[leetcode]Interleaving String @ Python

原题地址:https://oj.leetcode.com/problems/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.Whe

Interleaving String leetcode java

题目: 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. 题解: 这道题还是像

[LeetCode] Interleaving String [30]

题目 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. 原题链接(点我) 解题

leetcode -day27 Recover Binary Search Tree &amp; Interleaving String

1.  Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space solut

【leetcode】Interleaving String

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

【动态规划+滚动数组】Interleaving 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&quo