LeetCode:Interleaving String

原创 2016年05月30日 15:51:54

Interleaving String




Total Accepted: 50191 Total Submissions: 221528 Difficulty: Hard

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.

Subscribe to see which companies asked this question






















思路:动规

如:s1 = "aabcc",s2 = "dbbca";

s3 = "aadbbcbcac";


其状态转移过程为:

   0 1 2 3 4 5 6 7 8 10

s3 a a d b b c b c a c

-----------------------

     0 1 2 3 4 5

    s1 a a b c c

0 s2 T T T F F F

1 d  F F T T F F

2 b  F F T T T F

3 b  F F T F T T 

4 c  F F T T T F

5 a  F F F F T T


状态转移方程为:

if i==0 dp[i][j] = dp[i][j-1] && s1[j-1]==s3[i+j-1];

if j==0 dp[i][j] = dp[i-1][j] && s2[i-1]==s3[i+j-1];

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


c++ code:

class Solution {
public:
    bool isInterleave(string s1, string s2, string s3) {
        
        int len1 = s1.length();
        int len2 = s2.length();
        if(s3.length() != len1 + len2) return false;
        
        bool dp[len1+1][len2+1];
        
        for(int i=0;i<=len1;i++) {
            for(int j=0;j<=len2;j++) {
                if(i==0 && j==0)
                    dp[i][j] = true;
                else if(i==0)
                    dp[i][j] = (dp[i][j-1] && s2[j-1]==s3[i+j-1]);
                else if(j==0)
                    dp[i][j] = (dp[i-1][j] && s1[i-1]==s3[i+j-1]);
                else
                    dp[i][j] = (dp[i][j-1] && s2[j-1]==s3[i+j-1])||(dp[i-1][j] && s1[i-1]==s3[i+j-1]);
            }
        }
        return dp[len1][len2];
    }
};


版权声明:本文为博主原创文章,转载请注明出处。

LeetCode: Interleaving String [097]

【题目】 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Gi...

leetcode: 97. Interleaving String 递归与DP两种算法

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s...

LeetCode 97 Interleaving String

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: ...

LeetCode-Interleaving String

class Solution { public: bool isInterleave(string s1, string s2, string s3) { // Start t...

[LeetCode] Interleaving String

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given...

Leetcode - 97.Interleaving String

Problem description: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.F...

LeetCode | Interleaving String

题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For exampl...

LeetCode 97. Interleaving String(字符串交织)

原题网址:https://leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is forme...
  • jmspan
  • jmspan
  • 2016年05月23日 00:39
  • 511

leetcode 97. Interleaving String

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given...

[LeetCode]Interleaving String

QuestionGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example, G...
  • byamao1
  • byamao1
  • 2016年11月16日 21:02
  • 50
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode:Interleaving String
举报原因:
原因补充:

(最多只允许输入30个字)