Sign in to follow this  

combinations

This topic is 4483 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i've been trying to make something to systematically find every combination of a string. In small strings ,like "sea", it seemse like all i have to do is swap the chracters around to find every combination (sea,sae,esa,eas,ase,aes). when i try this method with larger strings, "abcdef", it doesn't work. can anyone describe a way to check for every combination of a string of any length?

Share this post


Link to post
Share on other sites
Assuming you're using C++.

First sort the string with std::sort()
Then repeatedly call std::next_permutation() until it returns false.

Both functions can be found in the <algorithm> header.

Share this post


Link to post
Share on other sites
for a string of n characters a(n), a(0) can be at the first position with n different strings. a(1) can be ther first characters n times [...] a(n) can be the nth character n times.

For each of the possibilities above, remove the leading character and do the same with the resulting string, appending each one to a "removed characters" one.

[Edited by - jfclavette on October 8, 2005 8:51:04 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this