All possible permutations of a string - Total number of permutations of a given word taking all n letters is n!. For example, the word post will give 24 possible permutations inclusive of the original word (post).
post
pots
psot
psto
ptos
ptso
opts
opst
ostp
ospt
otsp
otps
spot
spto
sopt
sotp
stpo
stop
tpso
tpos
tosp
tops
tsop
tspo
This snippet here doesn't exactly output permutations of a string, but its indexes - which should correspond to the array indexes.
Now, its pretty obvious why I had the permutations written to a file instead of console output. 9! is 362,880 but 10! is 10 times 9! which is more than 3.5 million lines of text.
Update : I have ported the C code in Java with some modifications in the way output is handled. This is much safer than that big for loop.
PS: I wrote this half a decade ago but never documented it. I'll try to update it with an explanation soon.
1 comments:
Nice article
Post a Comment