lintcode-medium-Interleaving String

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

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

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

动态规划,用一个二维数据记录:s1前i个字符,s2前j个字符,是不是s3前i + j个字符的interleave

状态转移:

dp[i - 1][j]为true,且s1的下一个字符和s3的下一个字符相等,或者dp[i][j - 1]为true,且s2的下一个字符和s3的下一个字符相等,则dp[i][j]为true,否则dp[i][j]为false

public class Solution {
    /**
     * Determine whether s3 is formed by interleaving of s1 and s2.
     * @param s1, s2, s3: As description.
     * @return: true or false.
     */
    public boolean isInterleave(String s1, String s2, String s3) {
        // write your code here
        if(s1 == null && s2 == null && s3 == null)
            return true;
        if((s1 == null || s1.length() == 0) && s2.equals(s3))
            return true;
        if((s2 == null || s2.length() == 0) && s1.equals(s3))
            return true;

        int m = s1.length();
        int n = s2.length();
        int size3 = s3.length();

        if(m + n != size3)
            return false;

        boolean[][] dp = new boolean[m + 1][n + 1];

        dp[0][0] = true;

        for(int i = 1; i <= n; i++)
            if(s2.charAt(i - 1) == s3.charAt(i - 1) && dp[0][i - 1])
                dp[0][i] = true;
            else
                dp[0][i] = false;

        for(int i = 1; i <= m; i++)
            if(s1.charAt(i - 1) == s3.charAt(i - 1) && dp[i - 1][0])
                dp[i][0] = true;
            else
                dp[i][0] = false;

        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                if(dp[i - 1][j] && s1.charAt(i - 1) == s3.charAt(i + j - 1))
                    dp[i][j] = true;
                else if(dp[i][j - 1] && s2.charAt(j - 1) == s3.charAt(i + j - 1))
                    dp[i][j] = true;
                else
                    dp[i][j] = false;
            }
        }

        return dp[m][n];
    }
}
时间: 2024-11-10 13:36:29

lintcode-medium-Interleaving String的相关文章

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个是否interleav

【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

【动态规划+滚动数组】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&quo

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(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