1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| class Solution { public boolean isInterleave(String s1, String s2, String s3) { if (s1 == null || s2 == null || s3 == null) { return false; } if (s1.length() + s2.length() != s3.length()) { return false; } if (s1.length() == 0) { return s2.equals(s3); } if (s2.length() == 0) { return s1.equals(s3); } return helper(s1, 0, s2, 0, s3, 0); } private boolean helper(String s1, int i1, String s2, int i2, String s3, int i3) { if (i3 >= s3.length()) { return true; } boolean result = false; if (i1 < s1.length() && s1.charAt(i1) == s3.charAt(i3)) { result = result || helper(s1, i1 + 1, s2, i2, s3, i3 + 1); } if (result) { return result; } if (i2 < s2.length() && s2.charAt(i2) == s3.charAt(i3)) { result = result || helper(s1, i1, s2, i2 + 1, s3, i3 + 1); } return result; } }
|