// This program solves the string permutation problem import java.util.*; public class Permute { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("What String would you like to permute? "); String s = console.next(); System.out.println(); permute(s); } // print out all possible permutations of the given String // n -- n! permutations public static void permute(String s) { permute(s, ""); } // where have we been? chosen: partial solution // where are we going? s: what characters are left private static void permute(String s, String chosen) { if (s.length() == 0) { System.out.println(chosen); } else { // s.length() > 0 for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); permute(s.substring(0, i) + s.substring(i + 1), chosen + c); } } } }