Generate all permutation of a string in recursion in java -
this question has answer here:
- generating permutations of given string 41 answers
i know how create permutation of given string in recursive way.
lets string = "abcdefghijklmnopqrstxyz";
.
i generate string of length 5 (for example) a
using recursion,
meaning:
- aaaaa
- aaaab
- aaaba
- aaaca
- zabfg
thanks in advance.
first, store unique characters using hashmap or so, transfer them list, call chars, ease of use.
your recursive method building on string. when string reaches length 5, done, , want keep it. can return string or store in global list.
in case, assume list called permutations.
void generatepermutation(string current) { if (current.length == 5) { permutations.add(current); } else { (int = 0; < chars.size(); i++) { generatepermutation(current + chars.get(i)); } } }
Comments
Post a Comment