Skip to content

Instantly share code, notes, and snippets.

@pandey-adarsh147
Created December 30, 2013 21:55
Show Gist options
  • Save pandey-adarsh147/8188884 to your computer and use it in GitHub Desktop.
Save pandey-adarsh147/8188884 to your computer and use it in GitHub Desktop.
Generate all permutation of a string
public class GenerateAllPermutation {
public static void main(String... arg) {
Scanner scanner = new Scanner(System.in);
String sequence = scanner.nextLine();
generatePermutation("", sequence);
}
private static void generatePermutation(String prefix, String sequence) {
if(sequence.length() == 0) System.out.println(prefix);
for (int i = 0; i < sequence.length(); i++) {
generatePermutation(prefix + sequence.charAt(i), sequence.substring(0, i) + sequence.substring(i+1));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment