Print all permutations of a string in Java

Given a string str, the task is to print all the permutations of str. A permutation is an arrangement of all or part of a set of objects, with regard to the order of the arrangement. For instance, the words ‘bat’ and ‘tab’ represents two distinct permutation (or arrangements) of a similar three letter word.

Examples:

Input: str = “cd”
Output: cd dc



Input: str = “abb”
Output: abb abb bab bba bab bba

Approach: Write a recursive function that prints every permutation of the given string. Terminating condition will be when the passed string is empty.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print all the permutations
// of the given string
public class GFG {
  
    // Function to print all the permutations of str
    static void printPermutn(String str, String ans)
    {
  
        // If string is empty
        if (str.length() == 0) {
            System.out.print(ans + " ");
            return;
        }
  
        for (int i = 0; i < str.length(); i++) {
  
            // ith character of str
            char ch = str.charAt(i);
  
            // Rest of the string after excluding 
            // the ith character
            String ros = str.substring(0, i) + 
                         str.substring(i + 1);
  
            // Recurvise call
            printPermutn(ros, ans + ch);
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        String s = "abb";
        printPermutn(s, "");
    }
}

chevron_right


Output:

abb abb bab bba bab bba

When the permutations need to be distinct.

Examples:

Input: str = “abb”
Output: abb bab bba

Input: str = “geek”
Output: geek geke gkee egek egke eegk eekg ekge ekeg kgee kege keeg

Approach: Write a recursive function that print distinct permutations. Make a boolean array of size ’26’ which accounts the character being used. If the character has not been used then the recursive call will take place. Otherwise, don’t make any call. Terminating condition will be when the passed string is empty.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print all the permutations
// of the given string
public class GFG {
  
    // Function to print all the distinct
    // permutations of str
    static void printDistinctPermutn(String str, 
                                     String ans)
    {
  
        // If string is empty
        if (str.length() == 0) {
  
            // print ans
            System.out.print(ans + " ");
            return;
        }
  
        // Make a boolean array of size '26' which
        // stores false by default and make true 
        // at the position which alphabet is being
        // used
        boolean alpha[] = new boolean[26];
  
        for (int i = 0; i < str.length(); i++) {
  
            // ith character of str
            char ch = str.charAt(i);
  
            // Rest of the string after excluding 
            // the ith character
            String ros = str.substring(0, i) + 
                         str.substring(i + 1);
  
            // If the character has not been used 
            // then recursive call will take place. 
            // Otherwise, there will be no recursive
            // call
            if (alpha[ch - 'a'] == false)
                printDistinctPermutn(ros, ans + ch);
            alpha[ch - 'a'] = true;
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        String s = "geek";
        printDistinctPermutn(s, "");
    }
}

chevron_right


Output:

geek geke gkee egek egke eegk eekg ekge ekeg kgee kege keeg


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.