Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes finding combinations of string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "finding combinations of string" Watch "finding combinations of string" New topic

finding combinations of string

siva sankar
Ranch Hand

Joined: Jul 13, 2006
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


Thanks In Advance
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
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:
Please correct me if I'm mistaken.


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 ]

Studying for SCJP 6
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
For more information, Google "permutations and combinations".
[ April 03, 2007: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jagan Nambi

Joined: Jan 16, 2004
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.

marc weber

Joined: Aug 31, 2004
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.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
I agree. Here's the link:
subject: finding combinations of string
It's not a secret anymore!