-
Notifications
You must be signed in to change notification settings - Fork 17
/
KLengthWords.java
102 lines (77 loc) · 2.31 KB
/
KLengthWords.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// M - I
import java.io.*;
import java.util.*;
public class Main {
// cc - current character
// Levels - character
public static void solve(int cc, String ustr, int ssf, int ts, Character spots[]) {
if(cc == ustr.length()) {
if(ssf == ts) {
for(int i=0; i<spots.length; i++) {
System.out.print(spots[i]);
}
System.out.println();
}
return;
}
char ch = ustr.charAt(cc);
for(int i=0; i<spots.length; i++) {
if(spots[i] == null) {
spots[i] = ch;
solve(cc+1, ustr, ssf + 1, ts, spots);
spots[i] = null;
}
}
solve(cc+1, ustr, ssf, ts, spots);
}
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;
}
}
Character spots[] = new Character[k];
solve(0, ustr, 0,k,spots);
}
}
// M-II
import java.io.*;
import java.util.*;
public class Main {
// cs - current spot
public static void solve(int cs, int ts, String ustr, boolean used[], String asf) {
if(cs == ts) {
System.out.println(asf);
return;
}
for(int i=0; i<ustr.length(); i++) {
char ch = ustr.charAt(i);
if(used[i] == false) {
used[i] = true;
solve(cs+1, ts, ustr, used, asf + ch);
used[i] = false;
}
}
}
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;
}
}
boolean used[] = new boolean[ustr.length()];
solve(0, k, ustr, used, "");
}
}