Results 1 to 9 of 9
  1. #1
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default 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);
    
    			}
    	}
    }
    the result when executing this program is

    it
    ti

    but i also need "t & i" by themself

    any ideas?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Can you explain the logic you are using to generate the output?

  3. #3
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    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

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    How does that algorithm produce Strings shorter than the original?

  5. #5
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    thats what the problem is, it doesn't and i need to help making it do so.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Add additional calls to the method with 1 at a time, then 2 at a time, then 3 at a time etc

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by hoosierfan24 View Post
    thats what the problem is, it doesn't and i need to help making it do so.
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    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

  9. #9
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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,

    Jos
    alright got it thanks!

Similar Threads

  1. Replies: 1
    Last Post: 11-04-2010, 07:53 PM
  2. Help with Creating Array List of permutations
    By sparklezilla in forum Advanced Java
    Replies: 4
    Last Post: 05-26-2010, 03:30 AM
  3. array permutations
    By mtK in forum Advanced Java
    Replies: 4
    Last Post: 04-08-2010, 06:57 AM
  4. Permutations
    By m00nchile in forum New To Java
    Replies: 3
    Last Post: 03-25-2010, 09:58 PM
  5. Find all permutations of a number
    By matzahboy in forum New To Java
    Replies: 6
    Last Post: 12-02-2008, 04:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •