// Goals of this code to teach recursion
// Review:  static methods, polymorphic methods, debugging methods

import java.io.*;

public class Permute {
	private static final boolean DEBUG = true;
	

// print out all the permutations of a string
public static void permute(String s) {
	permute("", s);
}

public static void debug(String s) {
	if ( DEBUG == true ) {
		System.out.println("   debug: " + s);
	}
}


// helper method that is more general
public static void permute(String prefix, String s) {


	debug("prefix = " + prefix + ";  s = " + s);
	if (s.length() <= 1 ) {
		System.out.println(prefix + s);
	} else {
		// permute the 1st character of remainder, there is at least one
		for (int i = 0; i < s.length(); i++ ) {
			String remainder = s.substring(0, i) + s.substring(i+1);
			permute(prefix + s.charAt(i), remainder);
		} 
	}
	System.out.println();
}
	
	
	

public static void main ( String[] args ) {
	
	permute("1234");
}

}
	
