(1회차 시도 실패...)
import java.io.*;
import java.util.*;
/**
* 풀이 과정:
* - 고민과 풀이:
*
* - 문제 해결:
*
* 시간복잡도: O()
* 공간복잡도: O()
*
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
HashMap<Character, Integer> map = new HashMap<>();
PriorityQueue<Integer>[] alpha = new PriorityQueue[26];
for (int j = 0; j < 26; j++) {
alpha[j] = new PriorityQueue<>();
}
int small = Integer.MAX_VALUE;
int big = -1;
char[] input = br.readLine().toCharArray();
int k = Integer.parseInt(br.readLine());
for (int j = 0; j < input.length; j++) {
if(!map.containsKey(input[j])){
alpha[input[j] - 'a'].add(j);
map.put(input[j], 1);
}else{
map.put(input[j], map.get(input[j])+1);
int num = map.get(input[j]);
alpha[input[j] - 'a'].add(j);
if(num == k){
int ans = j - alpha[input[j] - 'a'].poll()+1;
map.put(input[j], num-1);
small = Math.min(small, ans);
big = Math.max(big, ans);
}
}
}
if(big == -1){
bw.write("-1\n");
}else{
bw.write(small+ " " + big + "\n");
}
}
br.close();
bw.close();
}
}
(2회차 시도 성공!)
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
HashMap<Character, Integer> map = new HashMap<>();
PriorityQueue<Integer>[] alpha = new PriorityQueue[26];
for (int j = 0; j < 26; j++) {
alpha[j] = new PriorityQueue<>();
}
int small = Integer.MAX_VALUE;
int big = -1;
char[] input = br.readLine().toCharArray();
int k = Integer.parseInt(br.readLine());
for (int j = 0; j < input.length; j++) {
if(!map.containsKey(input[j])){
alpha[input[j] - 'a'].add(j);
map.put(input[j], 1);
}else{
map.put(input[j], map.get(input[j])+1);
int num = map.get(input[j]);
alpha[input[j] - 'a'].add(j);
if(num == k){
int ans = j - alpha[input[j] - 'a'].poll()+1;
map.put(input[j], num-1);
small = Math.min(small, ans);
big = Math.max(big, ans);
}
}
}
if(k == 1){
bw.write("1 1\n");
}else if(big == -1){
bw.write("-1\n");
}else{
bw.write(small+ " " + big + "\n");
}
}
br.close();
bw.close();
}
}