File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments