Lintcode29 Interleaving String solution 题解

【题目描述】

Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2.

给出三个字符串:s1、s2、s3,判断s3是否由s1和s2交叉构成。

【题目链接】

http://www.lintcode.com/en/problem/interleaving-string/

【题目解析】

dp[i][j]表示s1前i个和s2前j个对s3前i+j个是否interleaving string。

首先初始化。遍历s1,初始化所有的dp[i][0]

再遍历s2,初始化所有的dp[0][j]

若s3的第i+j-1位和s1的第i位相等,则看dp[i-1][j]是否为true;同理,若s3的i+j-1位和s2的第j位相等,则看dp[i][j-1]是否为true。只要两种情况中的任意一种为true,则dp[i][j]为true。

【参考答案】

http://www.jiuzhang.com/solutions/interleaving-string/

时间: 2024-10-28 10:51:39

Lintcode29 Interleaving String solution 题解的相关文章

Lintcode53 Reverse Words in a String solution 题解

[题目描述] Given an input string, reverse the string word by word. 给定一个字符串,逐个翻转字符串中的每个单词. [题目链接] http://www.lintcode.com/en/problem/reverse-words-in-a-string/ [题目解析] 这道题让我们翻转字符串中的单词,题目中给了我们写特别说明,如果单词之间遇到多个空格,只能返回一个,而且首尾不能有单词,并且对C语言程序员要求空间复杂度为O(1),所以我们只能对

Leetcode:Interleaving String 解题报告

Interleaving StringGiven 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

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. 题解:DP问题. 用数组dp[i][

【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: 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

[LeetCode] Interleaving String(dp)

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. 分析:非常好的DP的训练题,

[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

40. 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 f