java - Generating all permutations of a given string -
what elegant way find permutations of string. e.g. ba
, ba
, ab
, abcdefgh
? there example java implementation?
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 { (int = 0; < n; i++) permutation(prefix + str.charat(i), str.substring(0, i) + str.substring(i+1, n)); } }
Comments
Post a Comment