![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2005
Posts: 2
Rep Power: 0
![]() |
Hey everybody I am new in Java town and some “people” told me that in case of panic I can come here for help.
Well I am in PANIC. :eek: The problem is that I can not think recursively that good and I am struggling with this little problem for some time now. I need to generate all combinations of uppercase letters when given a group of starting letters. For example the combination of the first three letters of the alphabet ABC will generate ABC, AB, BC, A, B, C ( each string is in lexicographical order ) the number of combinations is (2^n )- 1 where n is the number of letters. So if I am given 4 letters the letters will be the first four letters of the alphabet ABCD and it will result in 15 combinations. The problem needs to be solved recursively!! Now I don’t want you guys to just give me the code, since it will be unethical and it will not help me on the long run, but if one of you can put me on the right track I will surely appreciate it. Thank you in advance. ![]() |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Splitting it into an array of chars might help a little -- I find it easier to index that way. If you could be more specific as to how you need help, we may be able to help you better. Also, I don't know if you're allowed to use ArrayList, but if you are, that would probably help you tremendously if you combine it with a Collections.sort() method when sorting lexicographically.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Apr 2005
Posts: 2
Rep Power: 0
![]() |
Ok i put the string into an array the problem is how i do the recursion to group. I dont know if this will help some more but if i am doing 4 caracters i will have this result
A, AB, AC, AD, ABC, ABD, ABCD B, BC, BD, BCD, C, CD D, The format is not important, I put it like this so you guys can see it better. I was thinking to make first an array of strings or ch (i guess strings is better)that will hold the letters |0|1|2|3| |A|B|C|D| And then creat one more array of strings that will hold 2^4-1 = 15 combinations Now i am stuck with the recursion part of the program on how to make the combinations. |
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
take the first letter in the array and put it with all the ones after it in the array, then do the same with the second and third etc just alwyas going forward through the array and not in reverse,
hmmm the next part i would have those 2 letters you have stored and add a third letter etc to them making sure no repeats or seomthing like that, |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|