Skip to content

Instantly share code, notes, and snippets.

@jinwolf
Created December 5, 2015 06:14
Show Gist options
  • Select an option

  • Save jinwolf/578c549cfe518d2d791f to your computer and use it in GitHub Desktop.

Select an option

Save jinwolf/578c549cfe518d2d791f to your computer and use it in GitHub Desktop.
Java String Permutation
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