Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# finding combinations of string

siva sankar
Ranch Hand
Posts: 68
Hi All,

Please give me idea to find out the differeant combinations of a string i.e, for eg. String ="HELLO";
I need to get different combinations for it as

hello,elloh,llohe......etc.

Thanks In Advance
Regards
SIva.

Eric Daly
Ranch Hand
Posts: 143
Are you asking how to determine mathematically the number of different combinations of letters, given a particular string? I may very well be wrong, but isn't it the number of characters factorial? Say you had 5 characters, 5! equals 120. There are 120 different possible ways to rearrange the characters. Or take the sequence "123". 3 characters, so 3! equals 6:
123
132
213
231
312
321
Please correct me if I'm mistaken.

OR

If you are asking how to make an algorithm to rearrange the characters in a string I'm sure we'd be able to help you. If that's more like what you're asking, are you looking for any combinations, or do you want to find ones that are actual words? For that you'd probably compare each new string to a dictionary (or word list) to test if it's actually a word.
[ April 03, 2007: Message edited by: Eric Daly ]

Peter Chase
Ranch Hand
Posts: 1970
For more information, Google "permutations and combinations".
[ April 03, 2007: Message edited by: Peter Chase ]

fred rosenberger
lowercase baba
Bartender
Posts: 12127
30
it's more complicated than a factorial if you can have a repeated letter. (h) (e) (l-one) (l-two) (o) is the same as (h) (e) (l-two) (l-one) (o). You can still use the factorial to get an idea, but the actual number is less, depending on how many letters are repeated, and how many times each letter is repeated.

A good book on discrete math should help you figure it out, if you need to know the exact number, but you may not care.

Jagan Nambi
Greenhorn
Posts: 15
Hey Siva,

The solution to your question lies in implementing an algorithm on "generating anagrams" that you can implement within your Java code. A discussion on algorithms for generating anagrams is too complex and beyond the scope of this forum.

But books/articles written by gurus such as Jon Bentley or Knuth would give you an idea in this direction.

Regards,
Jags.

marc weber
Sheriff
Posts: 11343
Once you've clarified exactly what you want to do, I suggest carefully describing your approach (algorithm) in English first. Once this is done, use your written description to write the Java code. This will help prevent going down the wrong path and missing important details in your code.