bool isInterleave(string s1, string s2, string s3) { // Start typing your C/C++ solution below // DO NOT write int main() function if( s1.size() + s2.size() != s3.size() ) return false; // save space if( s2.size() > s1.size() ){ string temp = s1; s1=s2; s2=temp; } vector<bool> isInter(s2.size()+1, false); // initialize the first row for( int i = 0; i <= s2.size(); i++){ if( s2.substr(0,i) != s3.substr(0,i) ) break; isInter[i] = true; } // treat each inner loop's isInter as isInter[i][j], the isInter[i-1][j] is the same as the one // on the right side of the assign line inner most for( int i = 1; i <= s1.size(); i ++){ isInter[0] = ( s1.substr(0,i) == s3.substr(0,i) ); for( int j = 1; j <= s2.size(); j++){ isInter[j]=(s2[j-1]==s3[i+j-1] && isInter[j-1] ) || ( s1[i-1]==s3[i+j-1] && isInter[j] ); } } return isInter[s2.size()]; }
bool isInter(string &s1, string &s2, string &s3, int pos1, int pos2) { int len1 = s1.size(); int len2 = s2.size(); int len3 = s3.size(); int pos3 = pos1+pos2; if(pos3 == len3) return true; if(pos1< len1 && s1[pos1] ==s3[pos3]) { if(isInter(s1, s2, s3, pos1+1, pos2)) return true; } if(pos2< len2 && s2[pos2] ==s3[pos3]) { if(isInter(s1, s2, s3, pos1, pos2+1)) return true; } return false; }
bool isInterleave(string s1, string s2, string s3) { if( s1.size() + s2.size() != s3.size() ) return false; vector<vector<bool>> isInter( s1.size()+1, vector<bool>(s2.size()+1, false) ); isInter[0][0] = true; int it = 1; while( it <= s1.size() && s1[it-1] == s3[it-1] ){ isInter[it][0] = true; it ++; } it = 1; while( it <= s2.size() && s2[it-1] == s3[it-1] ){ isInter[0][it] = true; it ++; } for( int s1_it = 1; s1_it <= s1.size(); s1_it ++ ){ for( int s2_it = 1; s2_it <= s2.size(); s2_it ++ ){ // check if isInter[s1_it][s2_it] is true? // two cases char temp_char = s3[s1_it + s2_it -1] ; // if s1[s1_it-1] == s3[s1_it + s2_it -1], isInter[s1_it][s2_it] will be true // if isInter[s1_it - 1][s2_it] is true if( temp_char == s1[s1_it-1] ) isInter[s1_it][s2_it] = isInter[s1_it-1][s2_it]; if( temp_char == s2[s2_it-1] ) isInter[s1_it][s2_it] = isInter[s1_it][s2_it-1] || isInter[s1_it][s2_it]; } } return isInter[s1.size()][s2.size()]; }
bool isInterleave(string s1, string s2, string s3) { if(s1.size()+s2.size() != s3.size()) return false; return isInter(s1, s2, s3, 0, 0); }