Results 1 to 9 of 9
 05242011, 07:17 PM #1Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
help with permutations of a string
hello, im working on a word scrambling program that will generate all the possible formations of the inputed letters, i have most of the code done i just cant figure out how to solve the rest of it.
here is what i have so far
Java Code:public class Scramble { public static void main(String args[]) { permuteString("", "it"); } public static void permuteString(String beginningString, String endingString) { if (endingString.length() <= 1) System.out.println(beginningString + endingString); else for (int i = 0; i < endingString.length(); i++) { String newString = endingString.substring(0, i) + endingString.substring(i + 1); permuteString(beginningString + endingString.charAt(i), newString); } } }
it
ti
but i also need "t & i" by themself
any ideas?
 05242011, 08:02 PM #2
Can you explain the logic you are using to generate the output?
 05242011, 08:40 PM #3Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
it starts by printing out the word
that was first originally entered, then it switches the last and 2nd to last character then the 2nd to last and the 3rd to last and soo forth until that character is the 2nd character, then it does the same until the last charcter is now the 3rd.
once that process completes the original 2nd letter becomes the first and the original 1st becomes 2nd and the previous process repeats
then the original 3rd becomes 1st and the origianl 2nd becomes 3rd and the process repeats
let me know if you need more detail
 05242011, 08:53 PM #4
How does that algorithm produce Strings shorter than the original?
 05252011, 06:15 PM #5Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
thats what the problem is, it doesn't and i need to help making it do so.
 05252011, 06:26 PM #6
Add additional calls to the method with 1 at a time, then 2 at a time, then 3 at a time etc
 05252011, 06:28 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,271
 Blog Entries
 7
 Rep Power
 24
Your problem is a combined problem; one solution runs as follows:
1) generate all combinations C(i, n) for 1 <= i <= n, e.g for the String "abc" you generate the following:
C(1, 3): a, b, c
C(2, 3): ab, ac, bc
C(3, 3): abc
2) next you generate all permutations for all String above:
P(C(1, 3)): a, b, c
P(C(2, 3)): ab, ba, ac, ca, bc, cb
P(C(3, 3)): abc, acb, bac, bca, cab, cba
All the Strings from step 2) are the Strings that you want.
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 05252011, 06:33 PM #8Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
ok so the first time i can pass all the letters then then all the letters 1 then again with a different combo of letters untill its complete and store everything in the possible combinations array
 05252011, 06:35 PM #9Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
Similar Threads

Test for all empty Strings in LinkedHashMap<String,ArrayList<String>
By albertkao in forum New To JavaReplies: 1Last Post: 11042010, 07:53 PM 
Help with Creating Array List of permutations
By sparklezilla in forum Advanced JavaReplies: 4Last Post: 05262010, 02:30 AM 
array permutations
By mtK in forum Advanced JavaReplies: 4Last Post: 04082010, 05:57 AM 
Permutations
By m00nchile in forum New To JavaReplies: 3Last Post: 03252010, 09:58 PM 
Find all permutations of a number
By matzahboy in forum New To JavaReplies: 6Last Post: 12022008, 04:59 AM
Bookmarks