Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# how to find all possible combinations of charcters in string

Mike Gari
Greenhorn
Posts: 18
hi all ,

this the example , i have a string contains 4 chars (A,B,C,D). size =4 , now all possibel cimbinations = N! which N = length of the String
i want to find all possible combinations like
ABCD
ABDC
ACBD
ACDB

BACD
BCDA
BDAC
BDCA

AND etc ...... till we reached to N! .

Darryl Burke
Bartender
Posts: 5148
11
Not a great recommendation for the SCJP, that.

Peter Taucher
Ranch Hand
Posts: 174
What do you mean when you say 'find all combinations'?

I'd say all combinations could be 'found' in such way:

Mike Gari
Greenhorn
Posts: 18
hey Darry! ,

the question is algorithmic question it's not about strings ,it's a part of a huge algorithm currently implement it ,i've just simplified the questions to strings so you may understand what do i want ?

i want to find the best solution recursively or not recursive from the public
you dont' need to talk about recommendation in SCJP , i'm currently doing a SCJD FYI .

Hi Peter ,

i want to stor every solution as described in my last post in char array .it's not ABCD . it may be ABCDEFGJKSLDLFD .
the length is variable .

Campbell Ritchie
Sheriff
Posts: 50241
79
Peter Taucher wrote:I'd say all combinations could be 'found' in such way:
That will include duplicates, giving n^n results rather than n!

Mike Gari
Greenhorn
Posts: 18
if you mean abcd and dcba is a duplicate , this wrong .

David Newton
Author
Rancher
Posts: 12617
@alaa: No, he was referring to the difference between permutations (*not* combination, by the way) with, and without, repetition. Without repetition you get N!, with repetition you get N^2. The code shown is N^2, "duplicates" referring to the use of a single letter in more than one position.

Mike Gari
Greenhorn
Posts: 18
I mean you can reverse and but the size of each array should be N .
EX: let ABCD the string , ACB will not be accepted the length should be the same length of the original string ,So
it's not permutation , and we will get N!.

Henry Wong
author
Marshal
Posts: 21506
84
alaa salameh wrote:I mean you can reverse and but the size of each array should be N .
EX: let ABCD the string , ACB will not be accepted the length should be the same length of the original string ,So
it's not permutation , and we will get N!.

Actually, what you just described is exactly what a permutation is. Regardless...

alaa salameh wrote:
i want to find the best solution recursively or not recursive from the public
you dont' need to talk about recommendation in SCJP , i'm currently doing a SCJD FYI .

This is pretty much a standard problem for recursion to solve. In fact, what you described is a homework problem for recursion, in many cases.

So... IMHO, recursive is the best solution to this problem.

Henry

Lalit Mehra
Ranch Hand
Posts: 384
even with recursion it'll take quite a lengthy algorithm if you have variable string length

Mike Gari
Greenhorn
Posts: 18
find below the solution i have.

David Newton
Author
Rancher
Posts: 12617
Lalit Mehra wrote:even with recursion it'll take quite a lengthy algorithm if you have variable string length

Do you mean it'll take a long time (relatively speaking) to execute?

Mike Gari
Greenhorn
Posts: 18
yes it's just an algorithm and i need to find an better solution , is that the wrong forum i posted in ???

David Newton
Author
Rancher
Posts: 12617
Do it iteratively.

Ireneusz Kordal
Ranch Hand
Posts: 423
alaa salameh wrote:yes it's just an algorithm and i need to find an better solution , is that the wrong forum i posted in ???

You can find an overview of various combinatorial algorithms here:
http://cs.utsa.edu/~wagner/knuth/

Ganesh Gowtham
Ranch Hand
Posts: 225
Hi

Please try the below code ...

Campbell Ritchie
Sheriff
Posts: 50241
79
Ganesh Gowtham wrote:Hi

Please try the below code ...
I may be mistaken, in which case I shall happily restore the code, but I think simply giving an answer like that is not helpful to the original poster. Don't be annoyed, but I have pulled rank and deleted it.

I hope you don't mind, Rob, Robert and Jesper.

Sheriff
Posts: 14691
16
Campbell Ritchie wrote:I may be mistaken, in which case I shall happily restore the code, but I think simply giving an answer like that is not helpful to the original poster. Don't be annoyed, but I have pulled rank and deleted it.
I hope you don't mind, Rob, Robert and Jesper.

And we have FAQs supporting this policy : LetThemDoTheirOwnHomework and DontBeACodeMill.

Mike Gari
Greenhorn
Posts: 18
it's the same like my solution , but it's just replaced to be String , i've marked the post as solved before you post this answer .

Thanks all for your suggestions .

Ganesh Gowtham
Ranch Hand
Posts: 225
Campbell Ritchie wrote:
Ganesh Gowtham wrote:Hi

Please try the below code ...
I may be mistaken, in which case I shall happily restore the code, but I think simply giving an answer like that is not helpful to the original poster. Don't be annoyed, but I have pulled rank and deleted it.

I hope you don't mind, Rob, Robert and Jesper.

HI Campbell Ritchie ,

I thought might helpfull for him ,Since he might have already invested considerable time of this research ...

Correct me if i am wrong ....

David Newton
Author
Rancher
Posts: 12617

Ganesh Gowtham
Ranch Hand
Posts: 225
Ganesh Gowtham wrote:Hi

Please try the below code ...

Hi Campbell Ritchie ,

could you please delete this particular answer since it misleads all, Since in core algorithm it throws exception ....

David Newton
Author
Rancher
Posts: 12617
@Ganesh: that's the point.

Campbell Ritchie
Sheriff
Posts: 50241
79
Ganesh Gowtham wrote: . . . it throws exception ....
Did you read the name of the Exception class?

Mike Gari
Greenhorn
Posts: 18
Guys ,

I've seen method code when it has been posted and before has been changed to throw new DoHomeWorkException () ,
but i'm telling you it's the same code i posted earlier with some changes from point to string .

Koushik Ghosh
Greenhorn
Posts: 25
• 1
Try this code.. I have used hashset to avoid duplicate strings.

Rish Gupta
Greenhorn
Posts: 29
.