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,050 points
92 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,320 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 by Sirajul
• 25,700 points

Related Questions In Java

0 votes
2 answers

Listing all the subclasses of a specific class in Java

This is not possible to do using ...READ MORE

answered Aug 21 in Java by Sirajul
• 25,700 points
188 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 in Java by Sirajul
• 25,700 points
61 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,540 points
91 views
0 votes
2 answers

Counting no of Occurrence of a particular character inside a string in Java

We can find out the no. of ...READ MORE

answered Sep 7, 2018 in Java by Sushmita
• 6,880 points
254 views
0 votes
2 answers

Result of character addition in Java

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

answered Aug 22 in Java by Sirajul
• 25,700 points
50 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 in Java by Sirajul
• 25,700 points
63 views
0 votes
2 answers

Change date format in a Java string

You can use LocalDateTime#parse() or ZonedDateTime#parse(), to parse a String in ...READ MORE

answered Aug 14, 2018 in Java by Sushmita
• 6,880 points
76 views
0 votes
1 answer

Encode String to UTF-8

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

answered May 28, 2018 in Java by Rishabh
• 3,540 points
38 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,880 points
598 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 in Java by Sirajul
• 25,700 points
136 views