Get all the permutations of a string in Java

0 votes
What is the solution for getting all the permutations of a given string using a recursive method
May 10, 2018 in Java by Daisy
• 8,140 points
2,272 views

2 answers to this question.

0 votes

This is the recursive way of finding a solution for this program:

public static void permutation(String str) { 
    permutation("", str); 
}
private static void permutation(String pre, String str) {
    int num = str.length();
    if (num == 0) System.out.println(pre);
    else {
        for (int i = 0; i < num; i++)
            permutation(pre + str.charAt(i), str.substring(0, i) + str.substring(i+1, num));
    }
}
answered May 10, 2018 by sharth
• 3,370 points
0 votes

You could use recursion to do this.

  •  Try each of the letters in turn as the first letter and then find all the permutations of the remaining letters using a recursive call.

  •  The base case is when the input is an empty string the only permutation is the empty string

Here's how you could probably implement it:

public class PermutationsTest {
    public static void main(String[] args) throws Exception {
        String str = "sample";
        StringBuffer strBuf = new StringBuffer(str);
        doPerm(strBuf,0);
    }
    private static void doPerm(StringBuffer str, int index){
        if(index == str.length())
            System.out.println(str);            
        else { //recursively solve this by placing all other chars at current first pos
            doPerm(str, index+1);
            for (int i = index+1; i < str.length(); i++) {//start swapping all other chars with current first char
               swap(str,index, i);
               doPerm(str, index+1);
                swap(str,i, index);//restore back my string buffer
            }
        }
    }
    private  static void swap(StringBuffer str, int pos1, int pos2){
        char c1 = str.charAt(pos1);
        str.setCharAt(pos1, str.charAt(pos2));
        str.setCharAt(pos2, c1);
    }}   
answered Aug 21, 2019 by Sirajul
• 59,230 points

Related Questions In Java

+2 votes
3 answers

Listing all the subclasses of a specific class in Java

Try with BurningWave core library. Here an ...READ MORE

answered Dec 29, 2019 in Java by anonymous
• 460 points

edited Jan 9, 2020 by anonymous 14,638 views
0 votes
2 answers

What is the easiest way to iterate through the characters of a string in Java?

There are two approaches to this: for(int i ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,230 points
1,877 views
0 votes
0 answers

Unscramble Letters in Java - Get all possible combinations of the letters

I am currently an AP Computer Science ...READ MORE

Apr 13, 2022 in Java by Rahul
• 3,380 points
964 views
0 votes
1 answer

How do I create a Java string from the contents of a file?

If you're looking for an alternative that ...READ MORE

answered Apr 19, 2018 in Java by Rishabh
• 3,620 points
1,211 views
0 votes
2 answers

Result of character addition in Java

Binary arithmetic operations on char and byte ...READ MORE

answered Aug 22, 2019 in Java by Sirajul
• 59,230 points
4,728 views
0 votes
2 answers

How to convert byte array to String and STring to byte array?

The best possible way of conversion between byte[] and String is to ...READ MORE

answered Aug 22, 2019 in Java by Sirajul
• 59,230 points
3,634 views
0 votes
3 answers

Change date format in a Java string

The reason for the inaccuracy is because ...READ MORE

answered Feb 9, 2022 in Java by Soham
• 9,710 points
3,442 views
0 votes
1 answer

Encode String to UTF-8

String objects in Java use the UTF-16 ...READ MORE

answered May 29, 2018 in Java by Rishabh
• 3,620 points
1,418 views
0 votes
2 answers

How can I get the filenames of all files in a folder which may or may not contain duplicates

List<String> results = new ArrayList<String>(); File[] files = ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,920 points
1,939 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
2,355 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP