[Coding Made Simple] String Interleaving
Posted Push your limit!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Coding Made Simple] String Interleaving相关的知识,希望对你有一定的参考价值。
Given three strings, return true if third string is interleaving of first and second string.
By running some examples, it seems that as long as s1.length() + s2.length() == s3.length() and both s1 and s2 are common subsequences of s3,
s3 is interleaving of s1 and s2. However, this assumption is not correct as shown in the following counter example.
s1 = "aba", s2 = "aca", s3 = "abacaa", both s1 and s2 are common subsequence of s3, but s3 is not interleaving of s1 and s2. The problem lies in
that s1 and s2 have overlapping characters in the common subsequence portion of s3.
Solution 1. Recursion
Because we need to keep the relative order of characters of s1 and s2 in s3, we know that for the last character of s3 must be either from s1\'s or s2\'s
end if s3 is interleaving of s1 and s2. If this is not the case, we know for sure s3 is not interleaving of s1 and s2.
Based on the above observation, the following recursive formulas are derived.
Base case: all characters in s3 are checked already, return true.
For s1[0.....i], s2[0....j], s3[0... i + j + 1],
case 1: there are unused characters in s1, i >= 0 && s1[i] == s3[i + j + 1]
check if s3[0....i + j] is interleaving of s1[0...i - 1] and s2[0....j];
if case 1 is false, proceed to case 2.
case 2: there are unused characters in s2, j >= 0 && s2[j] == s3[i + j + 1]
check if s3[0....i + j] is interleaving of s1[0...i] and s2[0....j - 1];
if case 2 is false, we\' just checked all possibilities, so return false.
Similarly with a lot of recursive solutions, this solution has the overlapping subproblems issue.
For example, for s1[0, 6] and s2[0, 5], we have the following recursive tree.
s1[0,6] and s2[0,5]
s1[0,5] and s2[0,5] s1[0, 6] and s2[0,4]
s1[0,4] and s2[0, 5] s1[0,5] and s2[0,4] s1[0,5] and s2[0,4] s1[0,6] and s2[0,4]
The red subproblems are overlapped.
1 public class InterleavingString { 2 public boolean isInterleavingStringRecursion(String s1, String s2, String s3) { 3 if(s1.length() + s2.length() != s3.length()) { 4 return false; 5 } 6 return recursiveHelper(s1, s1.length() - 1, s2, s2.length() - 1, s3, s3.length() - 1); 7 } 8 private boolean recursiveHelper(String s1, int endIdx1, String s2, int endIdx2, String s3, int endIdx3) { 9 if(endIdx3 < 0) { 10 return true; 11 } 12 if(endIdx1 >= 0 && s1.charAt(endIdx1) == s3.charAt(endIdx3)) { 13 if(recursiveHelper(s1, endIdx1 - 1, s2, endIdx2, s3, endIdx3 - 1)) { 14 return true; 15 } 16 } 17 else if(endIdx2 >= 0 && s2.charAt(endIdx2) == s3.charAt(endIdx3)) { 18 if(recursiveHelper(s1, endIdx1, s2, endIdx2 - 1, s3, endIdx3 - 1)) { 19 return true; 20 } 21 } 22 return false; 23 } 24 public static void main(String[] args) { 25 String s1 = "aab", s2 = "axy", s3 = "aaxaby", s4 = "abaaxy"; 26 String s5 = "aabcc", s6 = "dbbca", s7 = "aadbbcbcac", s8 = "aadbbbaccc"; 27 InterleavingString test = new InterleavingString(); 28 System.out.println(test.isInterleavingStringRecursion(s1, s2, s3)); 29 System.out.println(test.isInterleavingStringRecursion(s1, s2, s4)); 30 System.out.println(test.isInterleavingStringRecursion(s5, s6, s7)); 31 System.out.println(test.isInterleavingStringRecursion(s5, s6, s8)); 32 } 33 }
Solution 2. Dynamic Programming
State: T[i][j]: whether s3[0 ~ i + j - 1] is interleaving of s1[0 ~ i - 1] and s2[0 ~ j - 1]
Function: same with the recursive solution.
Init: T[0][0] = true, as an empty string is interleaving of two empty strings
1 public boolean isInterleavingStringDp(String s1, String s2, String s3) { 2 if(s1.length() + s2.length() != s3.length()) { 3 return false; 4 } 5 boolean[][] T = new boolean[s1.length() + 1][s2.length() + 1]; 6 T[0][0] = true; 7 for(int i = 1; i < T.length; i++) { 8 T[i][0] = s1.charAt(i - 1) == s3.charAt(i - 1) && T[i - 1][0]; 9 } 10 for(int j = 1; j < T[0].length; j++) { 11 T[0][j] = s2.charAt(j - 1) == s3.charAt(j - 1) && T[0][j - 1]; 12 } 13 for(int i = 1; i < T.length; i++) { 14 for(int j = 1; j < T[0].length; j++) { 15 if(s1.charAt(i - 1) == s3.charAt(i + j - 1) && T[i - 1][j]) { 16 T[i][j] = true; 17 } 18 else if(s2.charAt(j - 1) == s3.charAt(i + j - 1) && T[i][j - 1]) { 19 T[i][j] = true; 20 } 21 else { 22 T[i][j] = false; 23 } 24 } 25 } 26 return T[s1.length()][s2.length()]; 27 }
Related Problems
以上是关于[Coding Made Simple] String Interleaving的主要内容,如果未能解决你的问题,请参考以下文章
[Coding Made Simple] Longest Increasing Subsequence
[Coding Made Simple] String Interleaving
[Coding Made Simple] Matrix Chain Multiplication
[Coding Made Simple] Burst Balloon