1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| import java.util.*;
class Program { public static boolean interweavingStrings(String one, String two, String three) { StringBuilder currentString = new StringBuilder(); return helper(currentString, one, two, three, 0, 0); }
public static boolean helper(StringBuilder current, String one, String two, String three, int posOne, int posTwo) { if (posOne == one.length()) { String result = current.toString() + two.substring(posTwo, two.length()); return result.equals(three); } if (posTwo == two.length()) { String result = current.toString() + one.substring(posOne, one.length()); return result.equals(three); } current.append(one.charAt(posOne)); boolean selectStringOne = helper(current, one, two, three, posOne + 1, posTwo); current.deleteCharAt(current.length() - 1); if (selectStringOne) return true;
current.append(two.charAt(posTwo)); boolean selectStringTwo = helper(current, one, two, three, posOne, posTwo + 1); current.deleteCharAt(current.length() - 1); return selectStringTwo; } }
|