If you are looking for an algorithm instead of a library, here is one : To get the next permutation from a given string, say "aecfdb", step 1 - find the first character from right that is lesser than its right element. In above example, its c (c < f > d > b) step 2 - find the least character on the right side of 'c' such that it is greater than 'c'. In above example, that would be 'd' step 3 - swap characters from step 1 and step 2 ... you get "abdfcb" step 4 - sort all characters to the right of the new position of character in step 2 .. you get "aedbcf".
so "aedbcf" is the next permutation from "aecfdb". Next few permutations are "aedbfc", "aedcbf", "aedcfb", ... If you can't find any character at step 1, just sort/reverse the array. You would go from "fedcba" to "abcdef". Once you reach your original string, you have passed all permutations. On 8 May 2011 22:24, Shoubhik <[email protected]> wrote: > Could somebody post an algorithm/code for obtaining permutations of an > n-letter string . > > -- > You received this message because you are subscribed to the Google Groups > "google-codejam" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/google-code?hl=en. > > -- You received this message because you are subscribed to the Google Groups "google-codejam" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-code?hl=en.
