0% found this document useful (0 votes)
3 views

Recursion program to print all permutations of a strings

Recursion program solution

Uploaded by

Dilip Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Recursion program to print all permutations of a strings

Recursion program solution

Uploaded by

Dilip Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Recursion program

Permutation of all characters of a string

class Main {
{ swap(chars, currentIndex, i);
// Utility function to swap two permutations(chars,
characters in a character array currentIndex + 1);
private static void swap(char[] swap(chars, currentIndex, i);
chars, int i, int j) }
{ }
char temp = chars[i];
chars[i] = chars[j]; public static void
chars[j] = temp; findPermutations(String str) {
}
// base case
// Recursive function to generate all if (str == null || str.length() == 0)
permutations of a string {
private static void return;
permutations(char[] chars, int }
currentIndex)
{ permutations(str.toCharArray(),
if (currentIndex == chars.length - 0);
1) { }
System.out.println(String.value
Of(chars)); // generate all permutations of a
} string in Java
public static void main(String[] args)
for (int i = currentIndex; i < {
chars.length; i++) String str = "ABC";
findPermutations(str);
}
}

You might also like