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

【解析】

题意:有两个字符串s1和s2,每次从这两个字符串中的一个中取出一个字符(从前往后取),直到两个字符串都取完,这样可以组成很多新的字符串,问s3是否属于这些新组成的字符串中的一个。

暴力递归法是最直接的想法,但会超时。

动态规划法最关键的是找动规方程,换句话说如何构造二维数组,以及二维数组dp[i][j]表示什么含义。

下面代码中 dp[i][j] 表示 s2 的前 i 个字符和 s1 的前 j 个字符是否匹配 s3 的前 i+j 个字符

初始化dp[0][0]=0,dp[0][j]表示s2取0个,即s1的前j个字符是否匹配s3的前j个字符;dp[i][0]表示s1取0个,即s2的前i个字符,是否匹配s3的前i个字符。

动规方程:dp[i][j] = dp[i - 1][j] && s2[i - 1] == s3[i + j - 1]  ||  dp[i][j - 1] && s1[i][j - 1] == s3[i + j - 1] ,意思是如果s2的前i-1个字符和s1的前j个字符已经和s3的前i+j-1个字符匹配且s2的第i个字符等于s3的第i+j个字符,或者s2的前i个字符和s1的前j-1个字符已经和s3的前i+j-1个字符匹配,且s1的第j个字符等于s3的第i+j个字符,那么s2的前i个字符和s1的前j个字符能否与s3的前i+j-1个字符匹配。

【Java代码】

public class Solution {
    public boolean isInterleave(String s1, String s2, String s3) {
        if (s1.length() + s2.length() != s3.length()) return false;

        boolean[][] dp = new boolean[s2.length() + 1][s1.length() + 1];
        dp[0][0] = true;
        for (int j = 1; j <= s1.length(); j++) {
            dp[0][j] = dp[0][j - 1] && (s1.charAt(j - 1) == s3.charAt(j - 1));
        }
        for (int i = 1; i <= s2.length(); i++) {
            dp[i][0] = dp[i - 1][0] && (s2.charAt(i - 1) == s3.charAt(i - 1));
        }

        for (int i = 1; i <= s2.length(); i++) {
            for ( int j = 1; j <= s1.length(); j++) {
                dp[i][j] = dp[i - 1][j] && (s2.charAt(i - 1) == s3.charAt(i + j - 1))
                        || dp[i][j - 1] && (s1.charAt(j - 1) == s3.charAt(i + j - 1));
            }
        }

        return dp[s2.length()][s1.length()];
    }
}

参考 https://leetcode.com/discuss/22726/dp-solution-in-java

时间: 2024-08-02 06:56:08

【LeetCode】Interleaving String 解题报告的相关文章

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. 问题 : 给定三个字符串 s1, s

Leetcode:Scramble String 解题报告

Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string,

[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]LRU Cache, 解题报告

题目 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

[LeetCode] Interleaving String [30]

题目 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(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 [097]

[题目] 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. [题意] 给定字符