关闭

LeetCode:Interleaving String

标签: LeetCodeInterleaving String
554人阅读 评论(0) 收藏 举报
分类:

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];
    }
};


0
0
查看评论

leetcode_question_97 Interleaving String

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 =&...
  • doc_sgl
  • doc_sgl
  • 2013-09-15 22:08
  • 6367

97 - Interleaving String

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 =&#...
  • hellochenlu
  • hellochenlu
  • 2016-03-14 09:39
  • 652

动态规划——字符串的交错组成(interleaving-string)

题目描述:要求是判断一个字符串能不能由两个字符串按照它们自己的顺序,每次挑取两个串中的一个字符来构造出来。 For example, Given: s1 ="aabcc", s2 ="dbbca", When s3 ="aadbbcbcac&quo...
  • jingsuwen1
  • jingsuwen1
  • 2016-05-21 21:53
  • 471

97.Interleaving String

思路:动态规划。dp[m][n]表示是s1[0~m-1]和s2[0~n-1]能否构成s3[0~m+n-1]。那么很显然,递推式是dp[m][n]=((dp[m-1][n]&&s1[m-1]==s3[m+n-1])||(dp[m][n-1]&&s2[n-1]==s3[m...
  • wscdylzjy
  • wscdylzjy
  • 2015-05-19 00:10
  • 265

[leetcode-97]Interleaving String(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 = “aadbb...
  • zdavb
  • zdavb
  • 2015-08-11 10:24
  • 492

【LeetCode】Interleaving String 解题报告

【题目】 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given...
  • ljiabin
  • ljiabin
  • 2015-03-21 18:50
  • 2797

Interleaving String 一个月前没做出来的题目,闷了三天终于闷出来了

Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ex...
  • todedu
  • todedu
  • 2014-03-19 03:26
  • 505

LeetCode 97 Interleaving String

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 =...
  • mlweixiao
  • mlweixiao
  • 2014-10-09 13:17
  • 1076

[LeetCode] 97. Interleaving String

LeetCode 动态规划
  • hiimdaosui
  • hiimdaosui
  • 2016-08-17 12:57
  • 181

leetcode之Interleaving String

转自:http://blog.unieagle.net/2012/09/29/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Ainterleaving-string%EF%BC%8C%E4%BA%8C%E7%BB%B4%E5%8A%A8%E6%80%81%E8%A7%84%E5...
  • daisyZH
  • daisyZH
  • 2013-09-09 17:40
  • 571
    个人资料
    • 访问:389261次
    • 积分:6781
    • 等级:
    • 排名:第4130名
    • 原创:321篇
    • 转载:1篇
    • 译文:0篇
    • 评论:34条
    博客专栏
    最新评论