Leetcode 97, Interleaving String
2016-12-11 11:15
323 查看
public boolean isInterleave(String s1, String s2, String s3) {
if(s3.length() != s1.length() + s2.length()) {
return false;
}
int m = s1.length();
int n = s2.length();
boolean [][] dp = new boolean [m+1][n+1];
dp[0][0]=true;
for(int i = 1; i<=m; i++){
if(s1.charAt(i-1)==s3.charAt(i-1)){
dp[i][0] = dp[i-1][0];
}
}
for(int i = 1; i<=n; i++){
if(s2.charAt(i-1)==s3.charAt(i-1)){
dp[0][i]=true;
}
}
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
char c = s3.charAt(i+j-1);
if(c == s1.charAt(i-1)){
if(dp[i-1][j]){
dp[i][j] = true;
}
}
if(c == s2.charAt(j-1)){
if(dp[i][j-1]){
dp[i][j] = true;
}
}
}
}
return dp[m]
;
}
if(s3.length() != s1.length() + s2.length()) {
return false;
}
int m = s1.length();
int n = s2.length();
boolean [][] dp = new boolean [m+1][n+1];
dp[0][0]=true;
for(int i = 1; i<=m; i++){
if(s1.charAt(i-1)==s3.charAt(i-1)){
dp[i][0] = dp[i-1][0];
}
}
for(int i = 1; i<=n; i++){
if(s2.charAt(i-1)==s3.charAt(i-1)){
dp[0][i]=true;
}
}
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
char c = s3.charAt(i+j-1);
if(c == s1.charAt(i-1)){
if(dp[i-1][j]){
dp[i][j] = true;
}
}
if(c == s2.charAt(j-1)){
if(dp[i][j-1]){
dp[i][j] = true;
}
}
}
}
return dp[m]
;
}
相关文章推荐
- 【leetcode】String——Interleaving String(97)
- leetcode[97]Interleaving String
- leetcode_question_97 Interleaving String
- leetcode[97] Interleaving String
- LeetCode 97 Interleaving String
- Leetcode-97 Interleaving String
- [leetcode 97]Interleaving String
- LeetCode 97(Interleaving String)Java
- LeetCode 97 Interleaving String
- LeetCode(97) Interleaving String(动态规划)
- LeetCode[97]::Interleaving String C++
- LeetCode-97-Interleaving String DP 记忆化搜索
- LeetCode(97) Interleaving String
- leetcode_[python/C++]_97_Interleaving String_动态规划
- LeetCode(97) Interleaving String
- Leetcode 97 Interleaving String
- LeetCode 97 Interleaving String
- LeetCode 97 Interleaving String(Python详解及实现)
- LeetCode力扣之97. Interleaving String
- [leetcode] 97 Interleaving String