Interleaving strings

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: Input: AB and CD 

Output: ABCD, ACBD, ACDB, CABD, CADB, CDAB

public static void interleave(String ab, String cd, String str) {
 
     if(ab.length() == 0) {
 
          str += cd;
 
          System.out.println(str);
 
          return;
 
     }
 
     interleave(ab.substring(1), cd, str + ab.charAt(0));
 
     interleave(cd.substring(1), ab, str + cd.charAt(0));
 
}

From: http://tacambridge.com/blog/?p=60

你可能感兴趣的:(Interleaving strings)