标签:判断 wrap site and length min render AC else
描述:给出三个字符串:s1、s2、s3,判断s3是否由s1和s2交叉构成。
样例:s1 = "aabcc" s2 = "dbbca"
- 当 s3 = "aadbbcbcac",返回 true.
- 当 s3 = "aadbbbaccc", 返回 false.
Java
1 public class Solution { 2 /** 3 * @param s1: A string 4 * @param s2: A string 5 * @param s3: A string 6 * @return: Determine whether s3 is formed by interleaving of s1 and s2 7 */ 8 public boolean isInterleave(String s1, String s2, String s3){ 9 // write your code here 10 int s1_len=s1.length(); 11 int s2_len=s2.length(); 12 int s3_len=s3.length(); 13 if(s1_len == 0){ 14 if(s2.equals(s3)) 15 return true; 16 else 17 return false; 18 } 19 if(s2_len == 0){ 20 if(s1.equals(s3)) 21 return true; 22 else 23 return false; 24 } 25 int visited[][] = new int[s1_len + 1][s2_len + 1]; 26 visited[0][0]=1; 27 for(int i = 0;i < s1_len;i++){ 28 if(s1.charAt(i) == s3.charAt(i)) 29 visited[i + 1][0] = 1; 30 else 31 break; 32 } 33 for(int j = 0;j < s2_len;j++){ 34 if(s2.charAt(j) == s3.charAt(j)) 35 visited[0][j + 1] = 1; 36 else 37 break; 38 } 39 for(int i = 1;i < visited.length;i++){ 40 for(int j = 1;j < visited[0].length;j++){ 41 int row = i - 1; 42 int col = j - 1; 43 if((visited[i][j - 1] == 1 && 44 s2.charAt(col) == s3.charAt(row + col + 1)) || 45 (visited[i - 1][j] == 1 && 46 s1.charAt(row) == s3.charAt(row + col + 1))) 47 visited[i][j]=1; 48 } 49 } 50 if(visited[visited.length - 1][visited[0].length - 1] == 1) 51 return true; 52 return false; 53 } 54 }
标签:判断 wrap site and length min render AC else
原文地址:https://www.cnblogs.com/wangcj2015/p/9112480.html