Caso # | Resultado | Tiempo | Memoria |
---|---|---|---|
#1 |
Correcto
|
0.137 s | 14 KBi |
#2 |
Correcto
|
0.134 s | 17 KBi |
#3 |
Correcto
|
0.129 s | 13 KBi |
#4 |
Correcto
|
0.186 s | 12 KBi |
#5 |
Correcto
|
0.14 s | 13 KBi |
#6 |
Correcto
|
0.178 s | 12 KBi |
#7 |
Correcto
|
0.125 s | 14 KBi |
#8 |
Correcto
|
0.241 s | 16 KBi |
#9 |
Correcto
|
0.132 s | 13 KBi |
#10 |
Correcto
|
0.356 s | 18 KBi |
#11 |
Correcto
|
0.533 s | 21 KBi |
#12 |
Correcto
|
0.285 s | 20 KBi |
#13 |
Correcto
|
0.493 s | 20 KBi |
#14 |
Correcto
|
0.432 s | 19 KBi |
#15 |
Correcto
|
0.375 s | 18 KBi |
#16 |
Correcto
|
0.487 s | 18 KBi |
#17 |
Correcto
|
0.315 s | 19 KBi |
#18 |
Correcto
|
0.377 s | 19 KBi |
#19 |
Correcto
|
0.36 s | 17 KBi |
#20 |
Correcto
|
0.367 s | 22 KBi |
#21 |
Correcto
|
0.546 s | 21 KBi |
#22 |
Correcto
|
0.345 s | 21 KBi |
#23 |
Correcto
|
0.294 s | 18 KBi |
#24 |
Correcto
|
0.465 s | 20 KBi |
import java.util.*; public class Main { public static void main(String[] args) { // Substring más larga con máximo K caracteres diferentes // "codeo" // 3 Scanner scanner = new Scanner(System.in); final String input = scanner.next(); final int k = scanner.nextInt(); //Solution here boy int result = 0; int i = 0; final Map<Character, Integer> freqMap = new HashMap<>(); for(int j = 0; j < input.length(); j++) { char rightChar = input.charAt(j); freqMap.put(rightChar, freqMap.getOrDefault(rightChar, 0) + 1); while(freqMap.size() > k) { char leftChar = input.charAt(i); freqMap.put(leftChar, freqMap.getOrDefault(leftChar, 0) - 1); if(freqMap.get(leftChar) == 0) { freqMap.remove(leftChar); } i++; } result = Math.max(result, j - i + 1); } System.out.println(result); scanner.close(); } }