Search⌘ K
AI Features

Solution Review: Permutations of a String

Explore the recursive approach to generating all permutations of a string. Learn to identify base and recursive cases, use swapping techniques, and track recursion flow effectively in Java code.

Solution: Permutations of a String

Java
class Solution {
public static void permutations(char[] array, int length) {
if (length == 1) {
System.out.println(array);
return;
}
else {
for (int i = 0; i < length; i++) {
swap(array, i, length-1);
permutations(array, length-1);
swap(array, i, length-1);
}
}
}
public static void swap(char[] array, int i, int j) {
char c;
c = array[i];
array[i] = array[j];
array[j] = c;
}
public static void main( String args[] ) {
char[] input = {'a', 'b', 'b', 'a'};
permutations(input, input.length);
}
}

Understanding the

...