-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoString.java
55 lines (54 loc) · 1.34 KB
/
SoString.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
import java.io.*;
import java.util.*;
public class SoString {
public static void main(String[] args) {
try {
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
int k = Integer.parseInt(f.readLine());
String q = f.readLine();
boolean found;
if (k > q.length())
System.out.println("NO");
else if (k == 1) {
System.out.println("YES");
System.out.println(q);
}
else {
found = k <= charCount(q);
if (found) {
System.out.println("YES");
printStrings(q, k);
}
else {
System.out.println("NO");
}
}
f.close();
System.exit(0);
}
catch (IOException e) {
e.printStackTrace();
}
}
private static void printStrings(String s, int k) {
int[] indices = new int[k];
int i = 1;
for (int j = 1; j < s.length(); j++) {
if (s.charAt(j) != s.charAt(indices[i - 1]) && i < k) {
indices[i] = j;
i++;
}
}
for (int j = 1; j < indices.length; j++) {
System.out.println(s.substring(indices[j-1], indices[j]));
}
System.out.println(s.substring(indices[k-1]));
}
private static int charCount(String q) {
HashSet<Character> hash = new HashSet<Character>();
for (int i = 0; i < q.length(); i++) {
hash.add(q.charAt(i));
}
return hash.size();
}
}