The question simply asked if there is a recursive algorithm for the problem, and the answer is yes. To find it, find the base case and then the "step".
The main case is when one of the two lines is empty:
interleave(s1, "") = {s1}
interleave("", s2) = {s2}
Note that the order of the arguments does not matter, because
interleave("ab", "12") = {"ab12", "a1b2", "1ab2", "a12b", "1a2b", "12ab"} = interleave("12", "ab")
So, since the order does not matter, we consider recursion along the length of the first row.
So let's see how one case leads to the next. I just use a specific example, and you can generalize this to real code.
interleave("", "abc") = {"abc"}interleave("1", "abc") = {"1abc", "a1bc", "ab1c", "abc1"}interleave("12", "abc") = {"12abc", "1a2bc", "1ab2c", "1abc2", "a12bc", "a1b2c", "a1bc2", "ab12c", "ab1c2" "abc12" }
Therefore, every time we added a character to the first line, we formed a new result set, adding a new character to all possible positions in the old result set. Let's see how exactly we formed the third result above the second. How did each element in the second result turn into elements in the third result when we added β2β?
- "1abc" => "12abc", "1a2bc", "1ab2c", "1abc2"
- "a1bc" => "a12bc", "a1b2c", "a1bc2"
- "ab1c" => "ab12c", "ab1c2"
- "abc1" => "abc12"
Now look at things like this:
- "1abc" => {1 w | w = rotation ("2", "abc")}
- "a1bc" => {a1 w | w = rotation ("2", "bc")}
- "ab1c" => {ab1 w | w = rotation ("2", "c")}
- "abc1" => {abc1 w | w = interleave ("2", "")}
Although one or two examples do not prove the rule at all, in this case you should be able to conclude that such a rule. You will have a loop with recursive calls inside it.
This is actually a little more interesting for pure functional programming, but you noted the Java question.
Hope this is the beginning for you. If you are stuck, you can search the web for "alternating lines" or "alternating lists." There are several solutions.
EDIT:
Ok, I just wrote this stupid thing! It is very interesting to write these things in scripting languages, so I thought it would be great to see how it was in Java. Not as bad as I thought it would be! Here it is packaged as a whole Java application.
import java.util.ArrayList; import java.util.List; public class Interleaver { public static List<String> interleave(String s, String t) { List<String> result = new ArrayList<String>(); if (t.isEmpty()) { result.add(s); } else if (s.isEmpty()) { result.add(t); } else { for (int i = 0; i <= s.length(); i++) { char c = t.charAt(0); String left = s.substring(0, i); String right = s.substring(i); for (String u : interleave(right, t.substring(1))) { result.add(left + c + u); } } } return result; } public static void main(String[] args) { System.out.println(interleave("", "")); System.out.println(interleave("a", "")); System.out.println(interleave("", "1")); System.out.println(interleave("a", "1")); System.out.println(interleave("ab", "1")); System.out.println(interleave("ab", "12")); System.out.println(interleave("abc", "12")); System.out.println(interleave("ab", "1234")); } }