forked from jxixeun/jxix_java_algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ20437.java
More file actions
74 lines (68 loc) · 2.44 KB
/
Copy pathBOJ20437.java
File metadata and controls
74 lines (68 loc) · 2.44 KB
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
package baekjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class BOJ20437 {
static List<Integer>[] characters = new ArrayList[28];
private static int getShortestLength(int chIdx, int K) {
int i = 0;
int j = K-1;
int minLength = Integer.MAX_VALUE;
while (j < characters[chIdx].size()){
if (minLength > (characters[chIdx].get(j) - characters[chIdx].get(i)) + 1){
minLength = characters[chIdx].get(j) - characters[chIdx].get(i) + 1;
}
i++;j++;
}
return minLength;
}
private static int getLongestLength(int chIdx, int K) {
int i = 0;
int j = K-1;
int maxLength= Integer.MIN_VALUE;
while (j < characters[chIdx].size()){
if (maxLength < (characters[chIdx].get(j) - characters[chIdx].get(i)) +1){
maxLength = characters[chIdx].get(j) - characters[chIdx].get(i) + 1;
}
i++;j++;
}
return maxLength;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
for (int t = 0; t < T; t++) {
String W = br.readLine();
int K = Integer.parseInt(br.readLine());
for (int i = 0; i < 28; i++) {
characters[i] = new ArrayList<>();
}
for (int i = 0; i < W.length(); i++) {
characters[W.charAt(i) - 'a'].add(i);
}
int ans1 = Integer.MAX_VALUE;
int ans2 = Integer.MIN_VALUE;
for (int i = 0; i < 28; i++) {
if (characters[i].size() >= K) {
int result1 = getShortestLength(i, K);
int result2 = getLongestLength(i, K);
if (ans1 > result1) {
ans1 = result1;
}
if (ans2 < result2){
ans2 = result2;
}
}
}
if (ans1 == Integer.MAX_VALUE) {
sb.append(-1).append("\n");
} else {
sb.append(ans1).append(" ").append(ans2).append("\n");
}
}
System.out.print(sb);
}
}