Interleaving String

Given s1s2s3, 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.

最基础的做法: 硬比较,过不了大测试。

 1 public class Solution {
 2     public boolean isInterleave(String s1, String s2, String s3) {
 3         // Start typing your Java solution below
 4         // DO NOT write main() function
 5         if(s3.length() != s1.length() + s2.length()) return false;
 6         return check(s1, 0, s2, 0, s3, 0);
 7     }
 8     public boolean check(String s1, int p1, String s2, int p2, String s3, int p3){
 9         if(p3 == s3.length()) return true;
10         if(p1 < s1.length() && s3.charAt(p3) == s1.charAt(p1)){ 
11             if(p2 < s2.length() && s3.charAt(p3) == s2.charAt(p2))
12                 return check(s1, p1, s2, p2 + 1, s3, p3 + 1) || check(s1, p1 + 1, s2, p2, s3, p3 + 1);
13             else
14                 return check(s1, p1 + 1, s2, p2, s3, p3 + 1);
15         }else if(p2 < s2.length() && s3.charAt(p3) == s2.charAt(p2))
16             return check(s1, p1, s2, p2 + 1, s3, p3 + 1);
17         else return false;
18     }
19 }

第二遍: 采用二维DP来做。对于每个数 有一个关系:

isInterleaving(s1,len1,s2,len2,s3,len3)=(s3.lastChar == s1.lastChar)&& isInterleaving(s1,len1 -1,s2,len2,s3,len3 -1)||(s3.lastChar == s2.lastChar)&& isInterleaving(s1,len1,s2,len2 -1,s3,len3 -1)

 1 public class Solution {
 2     boolean[][] map = null;
 3     public boolean isInterleave(String s1, String s2, String s3) {
 4         // Start typing your Java solution below
 5         // DO NOT write main() function
 6         // char l3 = s3.length();
 7         int l2 = s2.length();
 8         int l1 = s1.length();
 9         if(s3.length() != l2 + l1) return false;
10         map = new boolean[l1 + 1][l2 + 1];
11         map[0][0] = true;
12         for(int i = 0; i < l1; i ++){
13             if(s1.charAt(i) == s3.charAt(i)) map[i + 1][0] = true;
14             else break;
15         }
16         for(int j = 0; j < l2; j ++){
17             if(s2.charAt(j) == s3.charAt(j)) map[0][j + 1] = true;
18             else break;
19         }
20         for(int i = 0; i < l1; i ++)
21             for(int j = 0; j < l2; j ++){
22                 char c1 = s1.charAt(i);
23                 char c2 = s2.charAt(j);
24                 char c3 = s3.charAt(i + j + 1);
25                 if(c1 == c3) map[i + 1][j + 1] = map[i][j + 1] || map[i + 1][j + 1];
26                 if(c2 == c3) map[i + 1][j + 1] = map[i + 1][j] || map[i + 1][j + 1];
27         }
28         return map[l1][l2];
29     }
30 }

 第三遍:

 1 public class Solution {
 2     public boolean isInterleave(String s1, String s2, String s3) {
 3         int l1 = s1.length(), l2 = s2.length(), l3 = s3.length();
 4         if(s1.length() + s2.length() != s3.length()) return false;
 5         boolean[][] arr = new boolean[l1 + 1][l2 + 1];
 6         arr[0][0] = true;
 7         for(int i = 0; i < l1 && arr[i][0]; i ++){
 8             if(s1.charAt(i) == s3.charAt(i)) arr[i + 1][0] = true; 
 9         }
10         for(int i = 0; i < l2 && arr[0][i]; i ++){
11             if(s2.charAt(i) == s3.charAt(i)) arr[0][i + 1] = true; 
12         }
13         for(int i = 1; i <= l1; i ++){
14             for(int j = 1; j <= l2; j ++){
15                 if(arr[i][j - 1] && s2.charAt(j - 1) == s3.charAt(i + j - 1)) arr[i][j] =  true;
16                 else if(arr[i - 1][j] && s1.charAt(i - 1) == s3.charAt(i + j - 1)) arr[i][j] = true;
17                 else arr[i][j] = false;
18             }
19         }
20         return arr[l1][l2];
21     } 
22 }

 

posted on 2013-09-25 06:59  Step-BY-Step  阅读(190)  评论(0编辑  收藏  举报

导航