Two Laptop Bag*
The moose likes Java in General and the fly likes how to find all possible combinations of charcters in string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to find all possible combinations of charcters in string" Watch "how to find all possible combinations of charcters in string" New topic
Author

how to find all possible combinations of charcters in string

Mike Gari
Greenhorn

Joined: Mar 28, 2010
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
ADCB
ADBC

BACD
BADC
BCAD
BCDA
BDAC
BDCA

AND etc ...... till we reached to N! .
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

Not a great recommendation for the SCJP, that.


luck, db
There are no new questions, but there may be new answers.
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
What do you mean when you say 'find all combinations'?

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


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
Mike Gari
Greenhorn

Joined: Mar 28, 2010
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

Joined: Oct 13, 2005
Posts: 38509
    
  23
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

Joined: Mar 28, 2010
Posts: 18

if you mean abcd and dcba is a duplicate , this wrong .
David Newton
Author
Rancher

Joined: Sep 29, 2008
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

Joined: Mar 28, 2010
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
Sheriff

Joined: Sep 28, 2004
Posts: 18764
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

even with recursion it'll take quite a lengthy algorithm if you have variable string length


http://plainoldjavaobject.blogspot.in
Mike Gari
Greenhorn

Joined: Mar 28, 2010
Posts: 18

find below the solution i have.
David Newton
Author
Rancher

Joined: Sep 29, 2008
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

Joined: Mar 28, 2010
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

Joined: Sep 29, 2008
Posts: 12617

Do it iteratively.
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
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

Joined: Mar 30, 2005
Posts: 225

Hi

Please try the below code ...



Thanks, Ganesh Gowtham
http://ganesh.gowtham.googlepages.com
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
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.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  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.


[My Blog]
All roads lead to JavaRanch
Mike Gari
Greenhorn

Joined: Mar 28, 2010
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

Joined: Mar 30, 2005
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

Joined: Sep 29, 2008
Posts: 12617

@Ganesh: Please read the FAQ entries that were pointed out--thanks!
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
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

Joined: Sep 29, 2008
Posts: 12617

@Ganesh: that's the point.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
Ganesh Gowtham wrote: . . . it throws exception ....
Did you read the name of the Exception class?
Mike Gari
Greenhorn

Joined: Mar 28, 2010
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

Joined: May 26, 2013
Posts: 25

Try this code.. I have used hashset to avoid duplicate strings.

Rish Gupta
Greenhorn

Joined: Sep 29, 2011
Posts: 28
.
 
jQuery in Action, 2nd edition
 
subject: how to find all possible combinations of charcters in string