Created
December 5, 2015 06:14
-
-
Save jinwolf/578c549cfe518d2d791f to your computer and use it in GitHub Desktop.
Java String Permutation
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 Permute { | |
| public static void main(String args[]) { | |
| permute(args[0]); | |
| } | |
| public static void permute(String list) { | |
| boolean[] used = new boolean[list.length()]; | |
| char[] in = list.toCharArray(); | |
| StringBuffer out = new StringBuffer(); | |
| int level = 0; | |
| doPermute(used, in, out, level); | |
| } | |
| public static void doPermute(boolean[] used, char[] in, StringBuffer out, int level) { | |
| if (level == in.length) { | |
| System.out.println(out.toString()); | |
| return; | |
| } | |
| for (int i=0; i < in.length; i++) { | |
| if (used[i]) { | |
| continue; | |
| } | |
| System.out.println(out.length()); | |
| out.append(in[i]); | |
| used[i] = true; | |
| doPermute(used, in, out, level + 1); | |
| used[i] = false; | |
| out.setLength(out.length() - 1); | |
| System.out.println(out); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment