Caso # | Resultado | Tiempo | Memoria |
---|---|---|---|
#1 |
Correcto
|
0.37 s | 16 KBi |
#2 |
Correcto
|
0.123 s | 27 KBi |
#3 |
Correcto
|
0.119 s | 12 KBi |
#4 |
Correcto
|
0.447 s | 15 KBi |
#5 |
Correcto
|
0.125 s | 29 KBi |
#6 |
Correcto
|
0.242 s | 14 KBi |
#7 |
Correcto
|
0.181 s | 12 KBi |
#8 |
Correcto
|
0.38 s | 18 KBi |
#9 |
Correcto
|
0.127 s | 19 KBi |
#10 |
Correcto
|
0.9 s | 23 KBi |
#11 |
Correcto
|
0.312 s | 19 KBi |
#12 |
Correcto
|
0.392 s | 20 KBi |
#13 |
Correcto
|
0.369 s | 17 KBi |
#14 |
Correcto
|
0.342 s | 21 KBi |
#15 |
Correcto
|
0.357 s | 21 KBi |
#16 |
Correcto
|
0.258 s | 19 KBi |
#17 |
Correcto
|
0.396 s | 21 KBi |
#18 |
Correcto
|
0.36 s | 19 KBi |
#19 |
Correcto
|
0.378 s | 19 KBi |
#20 |
Correcto
|
0.288 s | 18 KBi |
#21 |
Correcto
|
0.48 s | 18 KBi |
#22 |
Correcto
|
0.658 s | 18 KBi |
#23 |
Correcto
|
0.45 s | 17 KBi |
#24 |
Correcto
|
0.599 s | 19 KBi |
#25 |
Correcto
|
0.317 s | 16 KBi |
#26 |
Correcto
|
0.682 s | 19 KBi |
#27 |
Correcto
|
0.799 s | 23 KBi |
#28 |
Correcto
|
0.309 s | 23 KBi |
#29 |
Correcto
|
0.452 s | 20 KBi |
#30 |
Correcto
|
0.279 s | 19 KBi |
#31 |
Correcto
|
0.299 s | 19 KBi |
#32 |
Correcto
|
0.44 s | 20 KBi |
import java.util.*; //map > a:2, b:1, c:1 public class Main { public static void main(String[] args) { // Substring más corta con mínimo K caracteres diferentes // "abaca" // 3 -> 3 Scanner scanner = new Scanner(System.in); final String input = scanner.next(); final int k = scanner.nextInt(); //Solution here boy int result = Integer.MAX_VALUE; 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); } result = Math.min(result, j - i + 1); i++; } } System.out.println(result == Integer.MAX_VALUE ? -1 : result); scanner.close(); } }