-
Notifications
You must be signed in to change notification settings - Fork 0
/
dailycoding013.py
61 lines (49 loc) · 1.7 KB
/
dailycoding013.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def update_window(string, k, start, key_map):
while len(key_map.keys()) > k:
key_map[string[start]] -= 1
if key_map[string[start]] == 0:
key_map.pop(string[start], None)
start += 1
return start
def longest_k_substr(string, k):
counter = start = end = 0
prev_len = max_start = max_end = 0
key_map = {}
for i in range(len(string)):
# Check if char is in the map
if string[i] not in key_map:
key_map[string[i]] = 1
counter += 1
else:
key_map[string[i]] += 1
# Adjust the window bounds
if counter > k:
counter -= 1
start = update_window(string, k, start, key_map)
end += 1
# Update return variable if length exceeds current max
if len(string[start:end]) > prev_len:
prev_len = len(string[start:end])
max_start = start
max_end = end
return string[max_start:max_end]
def main():
test1 = ("abcba", 2, "bcb")
test2 = ("hello", 3, "hell")
test3 = ("acceptance", 4, "accep")
test4 = ("a", 1, "a")
test5 = ("abcdefg", 7, "abcdefg")
test6 = ("mississippi", 2, "ississi")
test7 = ("banana", 3, "banana")
if (longest_k_substr(*test1[:-1]) == test1[-1] and
longest_k_substr(*test2[:-1]) == test2[-1] and
longest_k_substr(*test3[:-1]) == test3[-1] and
longest_k_substr(*test4[:-1]) == test4[-1] and
longest_k_substr(*test5[:-1]) == test5[-1] and
longest_k_substr(*test6[:-1]) == test6[-1] and
longest_k_substr(*test7[:-1]) == test7[-1]):
print("Passed")
else:
print("Failed")
if __name__ == '__main__':
main()