I want to generate permutation of string of a given length k. Below is the code.
public class PermString {
private static void swap(char[] a, int i, int j){
char temp = a[i];
a[i] = a[j];
a[j] = temp;
}
private static void permute(char[] a, int n){
if(n ==1){
System.out.println(a);
}
for(int i=0; i<n ; i++){
swap(a, i, n-1);
permute(a, n-1);
swap(a, i, n-1);
}
}
public static void main(String[] args) {
String str = "ABCCCD";
char[] characters = str.toCharArray();
permute(characters, characters.length);
}
}
It generates the permutation of string correctly, I want to know how to code the condition for fixed size length, say k.