-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworsSel2.java
32 lines (28 loc) · 906 Bytes
/
worsSel2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Link - https://www.pepcoding.com/resources/data-structures-and-algorithms-in-java-levelup/recursion-and-backtracking/word-kselection-2-official/ojquestion
import java.util.*;
public class worsSel2{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
String str = s.nextLine();
int k = s.nextInt();
String ustr = "";
HashSet <Character> unique = new HashSet<>();
for(Character ch : str.toCharArray()){
if(!unique.contains(ch)){
unique.add(ch);
ustr += ch;
}
}
combination(ustr, 0, 0, k, "");
s.close();
}
public static void combination(String ustr, int idx, int iter, int k, String asf){
if(iter == k){
System.out.println(asf);
return;
}
for(int i = idx; i < ustr.length(); i++){
combination(ustr, i + 1, iter + 1, k, asf + ustr.charAt(i));
}
}
}