// Helene Martin, CSE143 public class Permutations { public static void main(String[] args) { permute("team"); } // Outputs all permutations of the given string, one per line. // For example, TEAM can be permuted into MEAT, META, TMEA, ... public static void permute(String s) { permute(s, ""); } private static void permute(String s, String soFar) { if (s.length() == 0) { System.out.println(soFar); } else { for (int i = 0; i < s.length(); i++) { // choose // char chosen = s.charAt(i); // soFar = soFar + chosen; // s = s.substring(0, i) + s.substring(i + 1); // explore (including choose and unchoose) permute(s.substring(0, i) + s.substring(i + 1), soFar + s.charAt(i)); // unchoose // soFar = soFar.substring(0, soFar.length() - 1); // s = s.substring(0, i) + chosen + s.substring(i); } } } }