Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package permutation;
- import java.util.ArrayList;
- import java.util.List;
- public class Permutation {
- List<String> results = new ArrayList<String>();
- public static void main(String [] args){
- String inputstring = "STB";
- char[] input = inputstring.toCharArray();
- int n = input.length-1;
- Permutation p = new Permutation();
- p.permute(input, 0, n);
- }
- /**
- * Recusive method to calculate the permutation of a given String.
- * A permutation is the rearrangement of elements of an ordered list
- * - further information @ http://mathworld.wolfram.com/Permutation.html
- * @param input
- * @param i
- * @param n
- */
- private void permute(char[] input, int i, int n){
- if(i == n){
- System.out.println(input);
- results.add(new String(input));
- }
- else{
- for(int j = i; j <= n; j++){
- swap(input, i, j);
- permute(input, i+1, n);
- swap(input, i, j);
- }
- }
- }
- private char[] swap(char[] input, int i, int j){
- char help = input[i];
- input[i] = input[j];
- input[j] = help;
- return input;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment