Interleaving

Given two strings .Print all the interleavings of the two strings.
Interleaving means that the if B comes after A .It should also come after A in the interleaved string.
example:
AB and CD
ABCD
ACBD
ACDB
CABD
CADB
CDAB

C++:
01 void printIntLv(string A, string B, int a, int b, string C)
02 {
03     if(a==A.length()){
04         C+=B.substr(b);
05         cout<<C<<endl;
06         return;
07     }
08     if(b==B.length()){
09         C+=A.substr(a);
10         cout<<C<<endl;
11         return;
12     }
13      printIntLv(A, B, a+1, b, C+A[a]);
14      printIntLv(A, B, a, b+1, C+B[b]);
15 }
Advertisements
This entry was posted in Backtrack, String. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s