#29 Interleaving String

原创 2016年08月30日 13:01:46

题目描述:

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

Example

For s1 = "aabcc", s2 = "dbbca"

  • When s3 = "aadbbcbcac", return true.
  • When s3 = "aadbbbaccc", return false.
Challenge 

O(n2) time or better

题目思路:

这题也是经典的dp之一。我们可以设dp[i][j]为s1前i-1个char和s2前j-1个char是s3 i+j-1的interleaving。那么,dp方程为dp[i][j]为true的选择有两种:一种是dp[i-1][j]为true并且s1的第i个char和s3的第i+j个char相等;还有一种是p[i][j-1]为true并且s2的第j个char和s3的第i+j个char相等。

Mycode(AC = 23ms):

class Solution {
public:
    /**
     * Determine whether s3 is formed by interleaving of s1 and s2.
     * @param s1, s2, s3: As description.
     * @return: true of false.
     */
    bool isInterleave(string s1, string s2, string s3) {
        // write your code here
        if (s1.size() + s2.size() != s3.size()) return false;
        
        vector<vector<bool>> dp(s1.size() + 1, vector<bool>(s2.size() + 1, false));
        
        dp[0][0] = true;
        // initialize dp[i][0] and dp[0][i]
        for (int i = 1; i <= s1.size(); i++) {
            dp[i][0] = dp[i - 1][0] && s3[i - 1] == s1[i - 1];
        }
        for (int i = 1; i <= s2.size(); i++) {
            dp[0][i] = dp[0][i - 1] && s3[i - 1] == s2[i - 1];
        }
        
        // do dp
        for (int i = 1; i <= s1.size(); i++) {
            for (int j = 1; j <= s2.size(); j++) {
                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[s1.size()][s2.size()];
    }
};


版权声明:三千微尘里,吾宁爱与憎

相关文章推荐

29.Interleaving String-交叉字符串(中等题)

翻转字符串 题目给出三个字符串:s1、s2、s3,判断s3是否由s1和s2交叉构成。 样例比如 s1 = “aabcc” s2 = “dbbca”当 s3 = “aadbbcbcac”,返回 tr...

leetcode Interleaving String

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

leetcode之Interleaving String

转自:http://blog.unieagle.net/2012/09/29/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Ainterleaving-string%EF%BC%...

Leetcode 97. Interleaving String

97. Interleaving String Total Accepted: 49560 Total Submissions: 219537 Difficulty: Hard ...

[leetcode]97. Interleaving String(Java)

https://leetcode.com/problems/interleaving-string/#/description Given s1, s2, s3, find wh...

LeetCode 97 — Interleaving String (C++ Python)

题目:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form...

【leetcode】Interleaving String

题目:给定三个串str1,str2,str3,判断str3是否是str1,str2的交叉字符串。 交叉字符串:两个字符串的字符交叉,组成新的字符串,要求属于原来字符串中的字符在新的生成的交叉字符串中...

LeetCode 55 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

LeetCode 动态规划

LeetCode 97. Interleaving String

1. 题目描述 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For examp...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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