97. Interleaving String Add to List

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.

解题思路:很经典的一道DP题。table[i][j]表示字符串s1的位置位i,s2的位置位j时能否用s1[0..i-1],s2[0...j-1]拼成s3[0...i+j-1]。初始化时table[0][0]=1表示全都为空时,可以完成。

class Solution {
public:
    bool isInterleave(string s1, string s2, string s3) {
        if(s1.length()+s2.length()!=s3.length())return false;
        vector<vector<int> >table(s1.length()+1,vector<int>(s2.length()+1,0));
        for(int i=0;i<s1.length()+1;i++)
            for(int j=0;j<s2.length()+1;j++)
                if(i==0&&j==0)table[i][j]=1;
                else if(i==0)table[i][j]=(table[i][j-1]&&s2[j-1]==s3[j-1]);
                else if(j==0)table[i][j]=(table[i-1][j]&&s1[i-1]==s3[i-1]);
                else table[i][j]=table[i-1][j]&&s1[i-1]==s3[i+j-1]||table[i][j-1]&&s2[j-1]==s3[i+j-1];
        return table[s1.length()][s2.length()];
    }
};
时间: 2024-11-05 15:17:27

97. Interleaving String Add to List的相关文章

[leedcode 97] 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. public class Solut

97. Interleaving String *HARD* -- 判断s3是否为s1和s2交叉得到的字符串

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. class Solution { p

97. 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. public bool IsInte

leetCode 97.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. 思路:此题刚开始有点想当然了

动态规划之97 Interleaving String

题目链接:https://leetcode-cn.com/problems/interleaving-string/description/ 参考链接:https://blog.csdn.net/u011095253/article/details/9248073 https://www.cnblogs.com/springfor/p/3896159.html 首先看到字符串的题目:  "When you see string problem that is about subsequence

【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

Leetcode dp Interleaving String

Interleaving String Total Accepted: 15409 Total Submissions: 79580My Submissions 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&q

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