Caso # | Resultado | Tiempo | Memoria |
---|---|---|---|
#1 |
Correcto
|
0.178 s | 15 KBi |
#2 |
Correcto
|
0.162 s | 16 KBi |
#3 |
Correcto
|
0.177 s | 15 KBi |
#4 |
Correcto
|
0.181 s | 15 KBi |
#5 |
Correcto
|
0.177 s | 16 KBi |
#6 |
Correcto
|
0.172 s | 15 KBi |
#7 |
Correcto
|
0.168 s | 16 KBi |
#8 |
Correcto
|
0.177 s | 16 KBi |
#9 |
Correcto
|
0.168 s | 16 KBi |
#10 |
Correcto
|
0.428 s | 23 KBi |
#11 |
Correcto
|
0.38 s | 21 KBi |
#12 |
Correcto
|
0.394 s | 24 KBi |
#13 |
Correcto
|
0.435 s | 22 KBi |
#14 |
Correcto
|
0.441 s | 23 KBi |
#15 |
Correcto
|
0.446 s | 25 KBi |
#16 |
Correcto
|
0.382 s | 22 KBi |
#17 |
Correcto
|
0.408 s | 22 KBi |
#18 |
Correcto
|
0.451 s | 24 KBi |
#19 |
Correcto
|
0.478 s | 23 KBi |
#20 |
Correcto
|
0.405 s | 21 KBi |
#21 |
Correcto
|
0.45 s | 21 KBi |
#22 |
Correcto
|
0.449 s | 22 KBi |
#23 |
Correcto
|
0.465 s | 22 KBi |
#24 |
Correcto
|
0.435 s | 22 KBi |
#25 |
Correcto
|
0.457 s | 22 KBi |
#26 |
Correcto
|
0.425 s | 21 KBi |
#27 |
Correcto
|
0.403 s | 23 KBi |
#28 |
Correcto
|
0.428 s | 24 KBi |
#29 |
Correcto
|
0.466 s | 24 KBi |
#30 |
Correcto
|
0.341 s | 21 KBi |
#31 |
Correcto
|
0.43 s | 23 KBi |
#32 |
Correcto
|
0.355 s | 22 KBi |
import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main{ public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); String s = sc.next(); int k = sc.nextInt(); sc.close(); Map<Character, Integer> m = new HashMap<Character, Integer>(); int current = 0; Character c; Integer in, min = Integer.MAX_VALUE; for (int i = 0; i < s.length(); i++) { c = s.charAt(i); in = m.get(c); if (in != null) { m.put(c, m.get(c) + 1); } else { m.put(c, 1); } while (m.size() >= k) { min = Math.min(min, 1 + i - current); c = s.charAt(current++); in = m.get(c); if (in != null) { m.put(c, in - 1); if (m.get(c) == 0) { m.remove(c); } } } } System.out.println(min != Integer.MAX_VALUE ? min : -1); } }