Given two strings str1 and str2, write a function that prints all interleavings of the given two strings. You may assume that all characters in both strings are different
Example:
Input: str1 = "AB", str2 = "CD" Output: ABCD ACBD ACDB CABD CADB CDAB
Solution:
public static void interleavings(String x, String y, String r){
if(x.length() >0){
if(y.length() >0){
interleavings(x.substring(1),y,r+x.charAt(0));
interleavings(x,y.substring(1),r+y.charAt(0));
}
else{
System.out.println(r+x);
}
}
else{
System.out.println(r+y);
}
}
http://www.geeksforgeeks.org/archives/17743
Thank you, this really helps!
ReplyDelete