0

Given a string, I need to print out all permutations of the string. How should I do that? I have tried

for(int i = 0; i<word.length();i++)
    {
        for(int j='a';j<='z';j++){
            word = word.charAt(i)+""+(char)j;
            System.out.println(word);   
        }
    }

Is there a good way about doing this?

ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37
LeatherFace
  • 478
  • 2
  • 11

3 Answers3

1

I'm not 100% sure that I understand what you are trying to do. I'm going to go by your original wording of the question and your comment to @ErstwhileIII's answer, which make me think that it's not really "permutations" (i.e. rearrangement of the letters in the word) that you are looking for, but rather possible single-letter modifications (not sure what a better word for this would be either), like this:

Take a word like "hello" and print a list of all "versions" you can get by adding one "typo" to it:

hello -> aello, bello, cello, ..., zello, hallo, hbllo, hcllo, ..., hzllo, healo, heblo, ...

If that's indeed what you're looking for, the following code will do that for you pretty efficiently:

public void process(String word) {
    // Convert word to array of letters
    char[] letters = word.toCharArray();
    // Run through all positions in the word
    for (int pos=0; pos<letters.length; pos++) {
        // Run through all letters for the current position
        for (char letter='a'; letter<='z'; letter++) {
            // Replace the letter
            letters[pos] = letter;
            // Re-create a string and print it out
            System.out.println(new String(letters));
        }
        // Set the current letter back to what it was
        letters[pos] = word.charAt(pos);
    }
}
Markus A.
  • 12,349
  • 8
  • 52
  • 116
0

OH .. to print out all permutations of a string, consider your algorithm first. What is the definition of "all permutations" .. for example:

  1. String "a" would have answer a only
  2. String "ab" would have answer: ab, ba
  3. String "abc" would have answer: abc acb, bca, bac, cba, cab

Reflect on the algorithm you would use (write it down in english) .. then translate to Java code

While not the most efficient, a recursive solution might be easiest to use (i.e. for a string of length n, go through each of the characters and follow that with the permutations of the string with that character removed).

ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37
  • I am suppose to do all permutations of the string, so for example after checking all the letters in the first slot, id have to move to the second. arse aase, abse, acse . Will your method be able to achieve that? – LeatherFace Mar 16 '14 at 23:23
0

EDIT: Ok... you changed your request. Permutations is a whole other story. I think this will help: Generating all permutations of a given string


Not sure what you are trying to do... Example 1 is to get the alphabet one letter next to another. Example 2 is to print whatever you gave us there as an example.

    //Example 1
    String word=""; //empty string
    for(int i = 65; i<=90;i++){ //65-90 are the Ascii numbers for capital letters
        word+=(char)i; //cast int to char
    }
    System.out.println(word);

    //Example 2
    String word="";
    for (int i=65;i<=90;i++){
        word+=(char)i+"rse";
        if(i!=90){ //you don't want this at the end of your sentence i suppose :)
            word+=", ";
        }
    }
    System.out.println(word);
Community
  • 1
  • 1
Aneliram
  • 51
  • 4