Created
June 7, 2013 16:28
-
-
Save maydayco/5730532 to your computer and use it in GitHub Desktop.
Interleaving String not clean code
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 | |
| if (s1.length() + s2.length() != s3.length()) | |
| return false; | |
| boolean[][] sol = new boolean[s1.length() + 1][s2.length() + 1]; | |
| sol[0][0] = true; | |
| for (int i = 1; i <= s3.length(); i++) { | |
| for (int j = 0; j <= s1.length() && j <= i; j++) { | |
| if (i - j > s2.length()) | |
| continue; | |
| boolean l1 = false; | |
| boolean l2 = false; | |
| if (j == 0) | |
| l1 = sol[j][i - j - 1] & s2.charAt(i - j - 1) == s3.charAt(i - 1); | |
| else if (i - j == 0) | |
| l2 = sol[j - 1][i - j] & s1.charAt(j - 1) == s3.charAt(i - 1); | |
| else { | |
| l1 = sol[j][i - j - 1] & s2.charAt(i - j - 1) == s3.charAt(i - 1); | |
| l2 = sol[j - 1][i - j] & s1.charAt(j - 1) == s3.charAt(i - 1); | |
| } | |
| sol[j][i - j] = l1 | l2; | |
| } | |
| } | |
| return sol[s1.length()][s2.length()]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment