-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsmallest_substring2.py
More file actions
executable file
·45 lines (34 loc) · 1.1 KB
/
smallest_substring2.py
File metadata and controls
executable file
·45 lines (34 loc) · 1.1 KB
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
#!/usr/bin/python
# vim: foldlevel=0
"""
Given two strings string1 and string2, find the smallest substring in string1 containing
all characters of string2 efficiently.
http://www.geeksforgeeks.org/find-the-smallest-window-in-a-string-containing-all-characters-of-another-string/
"""
def solution(arr, text):
"""
Time complexity: O(n)
>>> solution(['t', 'i', 's', 't'], "this is a test string")
't stri'
"""
def _has_all_chars(needed, tracker):
return all(x <= y for x, y in zip(needed, tracker))
res = text
count_needed = [0] * 256
for c in arr:
count_needed[ord(c)] += 1
lo = 0
window = [0] * 256
for hi in range(len(text)):
# Add new character to window
window[ord(text[hi])] += 1
# Remove trailing characters from window and check if we have a new smallest
while _has_all_chars(count_needed, window):
if hi-lo+1 < len(res):
res = text[lo:hi+1]
window[ord(text[lo])] -= 1
lo += 1
return res
if __name__ == "__main__":
import doctest
doctest.testmod()