Caso # | Resultado | Tiempo | Memoria |
---|---|---|---|
#1 |
Correcto
|
0.025 s | 3 KBi |
#2 |
Correcto
|
0.024 s | 3 KBi |
#3 |
Correcto
|
0.031 s | 3 KBi |
#4 |
Correcto
|
0.025 s | 3 KBi |
#5 |
Correcto
|
0.026 s | 3 KBi |
#6 |
Correcto
|
0.024 s | 3 KBi |
#7 |
Correcto
|
0.026 s | 3 KBi |
#8 |
Correcto
|
0.028 s | 3 KBi |
#9 |
Correcto
|
0.022 s | 7 KBi |
#10 |
Correcto
|
0.028 s | 3 KBi |
#11 |
Correcto
|
0.039 s | 3 KBi |
#12 |
Correcto
|
0.12 s | 3 KBi |
#13 |
Correcto
|
0.058 s | 3 KBi |
#14 |
Correcto
|
0.187 s | 8 KBi |
#15 |
Correcto
|
0.133 s | 3 KBi |
#16 |
Correcto
|
0.065 s | 3 KBi |
#17 |
Correcto
|
0.023 s | 3 KBi |
#18 |
Correcto
|
0.154 s | 3 KBi |
#19 |
Correcto
|
0.126 s | 3 KBi |
#20 |
Correcto
|
0.086 s | 3 KBi |
#21 |
Correcto
|
0.213 s | 3 KBi |
#22 |
Correcto
|
0.131 s | 3 KBi |
#23 |
Correcto
|
0.112 s | 3 KBi |
#24 |
Correcto
|
0.137 s | 3 KBi |
#25 |
Correcto
|
0.122 s | 3 KBi |
#26 |
Correcto
|
0.133 s | 3 KBi |
#27 |
Correcto
|
0.142 s | 3 KBi |
#28 |
Correcto
|
0.156 s | 3 KBi |
#29 |
Correcto
|
0.193 s | 3 KBi |
#30 |
Correcto
|
0.086 s | 3 KBi |
#31 |
Correcto
|
0.109 s | 3 KBi |
#32 |
Correcto
|
0.099 s | 3 KBi |
import math def shortest_substring_k_distinct(s, k = 1): if k == 1: return 1 shortest_substring_size = math.inf window_start = 0 char_freq = {} for window_end, current in enumerate(s): if current in char_freq: char_freq[current] = char_freq[current] + 1 else: char_freq[current] = 1 while len(char_freq) == k: shortest_substring_size = min(shortest_substring_size, (window_end + 1) - window_start) # Shorten window to the left prev = s[window_start] if char_freq[prev] == 1: char_freq.pop(prev) else: char_freq[prev] = char_freq[prev] - 1 window_start += 1 return -1 if math.isinf(shortest_substring_size) else shortest_substring_size s = input() k = int(input()) print(shortest_substring_k_distinct(s, k))