Skip to content

Commit c7a13c3

Browse files
authored
[20260225] BOJ / G5 / 문자열 게임 2 / 이준희
1 parent f707977 commit c7a13c3

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
int T = Integer.parseInt(br.readLine());
9+
10+
while (T-- > 0) {
11+
String W = br.readLine();
12+
int K = Integer.parseInt(br.readLine());
13+
14+
if (K == 1) {
15+
System.out.println("1 1");
16+
continue;
17+
}
18+
19+
List<Integer>[] pos = new ArrayList[26];
20+
for (int i = 0; i < 26; i++) pos[i] = new ArrayList<>();
21+
22+
for (int i = 0; i < W.length(); i++) {
23+
pos[W.charAt(i) - 'a'].add(i);
24+
}
25+
26+
int minLen = Integer.MAX_VALUE;
27+
int maxLen = -1;
28+
29+
for (int i = 0; i < 26; i++) {
30+
if (pos[i].size() < K) continue;
31+
32+
for (int j = 0; j <= pos[i].size() - K; j++) {
33+
int len = pos[i].get(j + K - 1) - pos[i].get(j) + 1;
34+
minLen = Math.min(minLen, len);
35+
maxLen = Math.max(maxLen, len);
36+
}
37+
}
38+
39+
if (minLen == Integer.MAX_VALUE) {
40+
System.out.println("-1");
41+
} else {
42+
System.out.println(minLen + " " + maxLen);
43+
}
44+
}
45+
}
46+
}
47+
```

0 commit comments

Comments
 (0)