Hire QA – Specialized in QA Recruitment, Technical Interviews and Testing Solutions

Write a program to print all permutations of String?

Category: Java

// 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);

        // Recursive call
        printPermutn(ros, ans + ch);
    }
}

// Driver code
public static void main(String[] args)
{
    String s = "abb";
    printPermutn(s, "");
}

}

Output

abb abb bab bba bab bba

Leave a Reply

Your email address will not be published. Required fields are marked *