I want to generate all possible teams of a group of n things taken k at a time, for example "abcd" = ab,ac,ad... without duplicates. I have written this, but it generates all permutations of a string. I have written a method to check if two strings have the same characters, but I don't know if this is the correct way.
package recursion;
import java.util.Arrays;
public class Permutations2 {
public static void main(String[] args) {
perm1("", "abcd");
System.out.println(sameChars("kostas","kstosa"));
}
private static void perm1(String prefix, String s) {
int N = s.length();
if (N == 0){
System.out.println(prefix);
}
else {
for (int i = 0; i < N; i++) {
perm1(prefix + s.charAt(i), s.substring(0, i) + s.substring(i+1, N));
}
}
}
private static boolean sameChars(String firstStr, String secondStr) {
char[] first = firstStr.toCharArray();
char[] second = secondStr.toCharArray();
Arrays.sort(first);
Arrays.sort(second);
return Arrays.equals(first, second);
}
}