Created
January 1, 2014 14:34
-
-
Save pandey-adarsh147/8208480 to your computer and use it in GitHub Desktop.
Generate permutation of 2 string in ordered form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GeneratePermutation2Strings { | |
public static void main(String... arg) { | |
Scanner scanner = new Scanner(System.in); | |
String s1 = scanner.nextLine(); | |
String s2 = scanner.nextLine(); | |
generateSequence("", s1, s2); | |
} | |
private static void generateSequence(String prefix, String s1, String s2) { | |
if(s1.length() + s2.length() == 0) { | |
System.out.println(prefix); | |
} | |
if(s1.length() > 0) { | |
String temp1 = s1.length() <= 1 ? "" : s1.substring(1, s1.length()); | |
generateSequence(prefix + (s1.length() > 0 ? s1.charAt(0) : ""),temp1 , s2); | |
} | |
if(s2.length() > 0) { | |
String temp2 = s2.length() <= 1 ? "" : s2.substring(1, s2.length()); | |
generateSequence(prefix + (s2.length() > 0 ? s2.charAt(0) : ""), s1, temp2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment