@@ -0,0 +1,1291 @@
+import java.util.Stack;%0A%0A/**%0A * %0A * One possible solution is to concatenate first and second string, then find%0A * all possible anagrams of that string, and finally check if the third string%0A * is present in one of the anagrams. Works! But the problem is generating the%0A * anagrams. Deteriorates the complexity.%0A * %0A * Another solution is to compromise of the space complexity a little, and make%0A * use of a stack as below.%0A */%0Apublic class CombineTwoStrings %7B%0A%09public static void main(String%5B%5D args) %7B%0A%09%09CombineTwoStrings cts = new CombineTwoStrings();%0A%09%09String one = %22rohit%22;%0A%09%09String two = %22deepthi%22;%0A%09%09String combined = %22rodehepitht%22;%0A%09%09System.out.print(cts.isValid(one, two, combined));%0A%09%7D%0A%0A%09public boolean isValid(String one, String two, String combined) %7B%0A%09%09Stack%3CCharacter%3E s = new Stack%3CCharacter%3E();%0A%09%09if ((one.length() + two.length()) != combined.length())%0A%09%09%09return false;%0A%09%09for (int i = combined.length() - 1; i %3E= 0; i--) %7B%0A%09%09%09s.push(combined.charAt(i));%0A%09%09%7D%0A%09%09int oneIndex = 0;%0A%09%09int twoIndex = 0;%0A%09%09while (!s.isEmpty() && oneIndex %3C one.length()%0A%09%09%09%09&& twoIndex %3C two.length()) %7B%0A%09%09%09char comparer = s.pop();%0A%09%09%09if (comparer == one.charAt(oneIndex))%0A%09%09%09%09oneIndex++;%0A%09%09%09else if (comparer == two.charAt(twoIndex))%0A%09%09%09%09twoIndex++;%0A%09%09%09else%0A%09%09%09%09return false;%0A%09%09%7D%0A%09%09return true;%0A%09%7D%0A%7D

Combine two strings solution using a stack.