• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to find all possible combinations of charcters in string

 
Mike Gari
Greenhorn
Posts: 18
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a great recommendation for the SCJP, that.
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you mean abcd and dcba is a duplicate , this wrong .
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21015
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even with recursion it'll take quite a lengthy algorithm if you have variable string length
 
Mike Gari
Greenhorn
Posts: 18
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
find below the solution i have.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do it iteratively.
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Please try the below code ...

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Ganesh: Please read the FAQ entries that were pointed out--thanks!
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Ganesh: that's the point.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh Gowtham wrote: . . . it throws exception ....
Did you read the name of the Exception class?
 
Mike Gari
Greenhorn
Posts: 18
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this code.. I have used hashset to avoid duplicate strings.

 
Rish Gupta
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic