-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordK1.java
41 lines (34 loc) · 1.19 KB
/
wordK1.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
33
34
35
36
37
38
39
40
41
// Link - https://www.pepcoding.com/resources/data-structures-and-algorithms-in-java-levelup/recursion-and-backtracking/words-klength-words-1-official/ojquestion
import java.io.*;
import java.util.*;
public class wordK1 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int k = Integer.parseInt(br.readLine());
HashSet<Character> unique = new HashSet<>();
String ustr = "";
for (char ch : str.toCharArray()) {
if (unique.contains(ch) == false) {
unique.add(ch);
ustr += ch;
}
}
selection(ustr, 0, k, 0, new char[k]);
}
public static void selection(String ustr, int idx, int ts, int cs, char [] asf){
if(idx == ustr.length()){
if(cs == ts)System.out.println(asf);
return;
}
char ch = ustr.charAt(idx);
for(int i = 0; i < ts; i++){
if(asf[i] == '\u0000'){
asf[i] = ch;
selection(ustr, idx + 1, ts, cs + 1, asf);
asf[i] = '\u0000';
}
}
selection(ustr, idx + 1, ts, cs, asf);
}
}