题目描述
给出三个字符串:s1、s2、s3,判断s3是否由s1和s2交叉构成。
样例1
输入:
"aabcc"
"dbbca"
"aadbbcbcac"
输出:
true
样例2
输入:
""
""
"1"
输出:
false
样例3
输入:
"aabcc"
"dbbca"
"aadbbbaccc"
输出:
false
解题思路
动态规划。
dp[i][j]代表由s1的前i个字母和s2的前j个字母是否能构成当前i+j个字母。
然后状态转移即可。(看第i+j+1个是否能被s1的第i+1个构成或被s2的第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 [][] interleaved = new boolean[s1.length() + 1][s2.length() + 1];
interleaved[0][0] = true;
for (int i = 1; i <= s1.length(); i++) {
if(s3.charAt(i - 1) == s1.charAt(i - 1) && interleaved[i - 1][0])
interleaved[i][0] = true;
}
for (int j = 1; j <= s2.length(); j++) {
if(s3.charAt(j - 1) == s2.charAt(j - 1) && interleaved[0][j - 1])
interleaved[0][j] = true;
}
for (int i = 1; i <= s1.length(); i++) {
for (int j = 1; j <= s2.length(); j++) {
if(((s3.charAt(i + j - 1) == s1.charAt(i - 1) && interleaved[i - 1][j]))
|| ((s3.charAt(i + j - 1)) == s2.charAt(j - 1) && interleaved[i][j - 1]))
interleaved[i][j] = true;
}
}
return interleaved[s1.length()][s2.length()];
}
}
C++题解
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) {
if (s1.length() + s2.length() != s3.length()) {
return false;
}
bool interleave[s1.length() + 1][s2.length() + 1];
interleave[0][0] = true;
for (int i = 1; i <= s1.length(); i++) {
interleave[i][0] = interleave[i - 1][0] && s1[i - 1] == s3[i - 1];
}
for (int i = 1; i <= s2.length(); i++) {
interleave[0][i] = interleave[0][i - 1] && s2[i - 1] == s3[i - 1];
}
for (int i = 1; i <= s1.length(); i++) {
for (int j = 1; j <= s2.length(); j++) {
interleave[i][j] = false;
if (s1[i - 1] == s3[i + j - 1]) {
interleave[i][j] = interleave[i][j] || interleave[i - 1][j];
}
if (s2[j - 1] == s3[i + j - 1]) {
interleave[i][j] = interleave[i][j] || interleave[i][j - 1];
}
}
}
return interleave[s1.length()][s2.length()];
}
};
python题解
class Solution:
"""
@params s1, s2, s3: Three strings as description.
@return: return True if s3 is formed by the interleaving of
s1 and s2 or False if not.
@hint: you can use [[True] * m for i in range (n)] to allocate a n*m matrix.
"""
def isInterleave(self, s1, s2, s3):
# write your code here
if s1 is None or s2 is None or s3 is None:
return False
if len(s1) + len(s2) != len(s3):
return False
interleave = [[False] * (len(s2) + 1) for i in range(len(s1) + 1)]
interleave[0][0] = True
for i in range(len(s1)):
interleave[i + 1][0] = s1[:i + 1] == s3[:i + 1]
for i in range(len(s2)):
interleave[0][i + 1] = s2[:i + 1] == s3[:i + 1]
for i in range(len(s1)):
for j in range(len(s2)):
interleave[i + 1][j + 1] = False
if s1[i] == s3[i + j + 1]:
interleave[i + 1][j + 1] = interleave[i][j + 1]
if s2[j] == s3[i + j + 1]:
interleave[i + 1][j + 1] |= interleave[i + 1][j]
return interleave[len(s1)][len(s2)]
来源:CSDN
作者:CN_wanku
链接:https://blog.csdn.net/qq_43233085/article/details/104071598