Pages

Monday, 9 April 2012

Generate all permutations of a string in Java

public  static void permutation(String str) { 
    permutation("", str); 
 }

 private static void permutation(String prefix, String str) {
    int n = str.length();
    if (n == 0) System.out.println(prefix);
    else {
        for (int i = 0; i < n; i++)
           permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n));
    }
}

1 comment:

  1. thanks for your solution.Here is another way to implement this http://techno-terminal.blogspot.in/2015/09/java-program-to-compute-all-permutation.html

    ReplyDelete