标签:style class blog code java http
Question:
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.
以
s1 = "aabcc"
,
s2 = "dbbca"
,
s3 = "aadbbcbcac"为例来看此题。
1 public class Solution { 2 public boolean isInterleave(String s1, String s2, String s3) { 3 if(s1.length()+s2.length()!=s3.length()) 4 return false; 5 boolean[][] state=new boolean[s1.length()+1][s2.length()+1]; 6 state[0][0]=true; 7 for(int i=1;i<=s1.length();i++){ 8 state[i][0]=state[i-1][0]&&(s1.charAt(i-1)==s3.charAt(i-1)); 9 } 10 for(int i=1;i<=s2.length();i++){ 11 state[0][i]=state[0][i-1]&&(s2.charAt(i-1)==s3.charAt(i-1)); 12 } 13 for(int i=1;i<=s1.length();i++){ 14 for(int j=1;j<=s2.length();j++){ 15 state[i][j]=(state[i-1][j]&&(s1.charAt(i-1)==s3.charAt(i+j-1)))||(state[i][j-1]&&(s2.charAt(j-1)==s3.charAt(i+j-1))); 16 } 17 } 18 return state[s1.length()][s2.length()]; 19 } 20 }
[Leetcode] Interleaving String,布布扣,bubuko.com
[Leetcode] Interleaving String
标签:style class blog code java http
原文地址:http://www.cnblogs.com/wolohaha/p/3781762.html