Java for LeetCode 097 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问题,JAVA实现如下:

	static 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];
		dp[0] = true;

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

		for (int i = 1; i <= s1.length(); i++) {
			dp[0] = dp[0] && s1.charAt(i - 1) == s3.charAt(i - 1);
			for (int j = 1; j <= s2.length(); ++j) {
				dp[j] = (dp[j] && s1.charAt(i - 1) == s3.charAt(i + j - 1))
						|| (dp[j - 1] && s2.charAt(j - 1) == s3.charAt(i + j
								- 1));
			}
		}
		return dp[s2.length()];
	}
时间: 2024-10-13 12:31:47

Java for LeetCode 097 Interleaving String的相关文章

097 Interleaving String

097 Interleaving String 这道题也是纯dp了 class Solution: # @param {string} s1 # @param {string} s2 # @param {string} s3 # @return {boolean} def isInterleave(self, s1, s2, s3): m = len(s1) n = len(s2) if m + n != len(s3): return False dp = [[False] * (n+1) f

【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 解题报告

[题目] 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. [解析] 题意:有

Java for LeetCode 087 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": To scramble the string, we may choose any non-leaf node and swap its two ch

【leetcode】 Interleaving String (hard)

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

LeetCode – Refresh – Interleaving String

Notes: 1. Even s3 is empty string, if s1 and s2 are emtpy, then it should be true. 2. Do not mess up the size of label. 1 class Solution { 2 public: 3 bool isInterleave(string s1, string s2, string s3) { 4 int l1 = s1.size(), l2 = s2.size(), l3 = s3.

[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