-
Notifications
You must be signed in to change notification settings - Fork 0
/
320.py
46 lines (33 loc) · 1.06 KB
/
320.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
# This problem was asked by Amazon.
# Given a string, find the length of the smallest window that
# contains every distinct character. Characters may appear more
# than once in the window.
#
# For example, given "jiujitsu", you should return 5, corresponding
# to the final five letters.
####
def smallest_window(inp):
reqd = len(set(inp))
n = len(inp)
begin = 0
end = 0
counts = {}
best = n
# iterate the end of the window
while end < n:
# increment the count of the letter
if not inp[end] in counts:
counts[inp[end]] = 0
counts[inp[end]] += 1
end += 1
# now shrink the beginning of the window as long as required
# characters are still present within the window
while len(counts) == reqd and counts[inp[begin]] > 1:
counts[inp[begin]] -= 1
begin += 1
# reassign the minimum window
if len(counts) == reqd:
best = min(best, end - begin)
return best
####
print(smallest_window("jiujitsu"))