Last active
December 14, 2015 18:09
-
-
Save guolinaileen/5127118 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public boolean isInterleave(String s1, String s2, String s3) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
int m=s1.length(); | |
int n=s2.length(); | |
int k=s3.length(); | |
if(k!=(n+m)) return false; | |
boolean S[][]=new boolean[m+1][n+1]; | |
S[0][0]=true; | |
for(int i=0; i<m; i++) | |
{ | |
if(s1.charAt(i)==s3.charAt(i)) S[i+1][0]=true; else break; | |
} | |
for(int j=0; j<n; j++) | |
{ | |
if(s2.charAt(j)==s3.charAt(j)) S[0][j+1]=true; else break; | |
} | |
for(int i=1;i<=m;++i) | |
{ | |
for(int j=1;j<=n;++j) | |
S[i][j]=(S[i][j-1]&&s3.charAt(i+j-1)==s2.charAt(j-1)) | |
||(S[i-1][j]&&s3.charAt(i+j-1)==s1.charAt(i-1)); | |
} | |
return S[m][n]; | |
} | |
} | |
/* | |
start from S[0][0] | |
case : S[i][j]=true | |
each step s3(i+j-1) | |
s1(i) s2(j-1) or s1(i-1) s2(j) | |
if(s3(i+j-1)==s1(i)) check S[i-1][j] | |
if(s3(i+j-1)==s2(j)) check S[i][j-1] | |
case: S[i][j]=false | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment