This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package algos; | |
public class Permutation { | |
private boolean[] used; | |
private StringBuilder out = new StringBuilder(); | |
private final String in; | |
public Permutation( final String str ){ | |
in = str; | |
used = new boolean[ in.length() ]; | |
} | |
public void permute( ){ | |
if( out.length() == in.length() ){ | |
System.out.println( out ); | |
return; | |
} | |
for( int i = 0; i < in.length(); ++i ){ | |
if( used[i] ) continue; | |
out.append( in.charAt(i) ); | |
used[i] = true; | |
permute(); | |
used[i] = false; | |
out.setLength( out.length() - 1 ); | |
} | |
} | |
public static void main(String[] args) { | |
Permutation ps = new Permutation("abc"); | |
ps.permute(); | |
} | |
} |