public class permutation{ public static void main(String[] args) { permutation(new String("STJ")); } public static void permutation(String str) { char[] in = str.toCharArray(); perm(in, 0); } void perm(char[] in, int start) { if (start == in.length - 1) { System.out.println(String.valueOf(in)); } else { for (int i = start; i < in.length; i++) { move(in, start, i); perm(in, start + 1); move(in, start, i); } } } void move(char[] in, int first, int second) { char temp; temp = in[first]; in[first] = in[second]; in[second] = temp; } }
输出结果:
STJ
SJT
TSJ
TJS
JTS
JST