-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortestSubstring.py
97 lines (60 loc) · 1.5 KB
/
shortestSubstring.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def get_shortest_unique_substring(arr, str):
# first check if we have all the characters in arr, if we don't, don't process, just return
# the empty string
ar={}
for i in arr:
ar[i]=1
for i in range(len(str)):
ar[str[i]]=0
valsum=0
for i in ar.values():
valsum+=i
if valsum!=0: return ""
# now that we know we have all the charaters in arr, try our solution
for i in arr:
ar[i]=len(str)-1
smallest=len(str)
ma = len(str)
mi = 0
for i in range(len(str)):
if str[i] in ar.keys():
# reassign our pointer
ar[str[i]]=i
if smallest>max(ar.values())-min(ar.values()):
ma = max(ar.values())
mi = min(ar.values())
smallest = ma-mi
print(ma)
print(mi)
print(str[mi:ma+1])
return str[mi:ma+1]
arr = ['x','y','z']
get_shortest_unique_substring(arr, "xyyzyzyx")
"""
arr = x, y, z
str = xyyzyzyx
1. look at sliding windows of str and see if we have
all the characters of arr
- sliding window has to be at least width of len(arr)
solution:
dictionary key: the char
value: 1
pointers for each character in arr
step through the example given
x - 0
y - 1 - 2
z - 3
(3- 0) + 1= 4
y - 4
greatest = 4, least x =0
5
z - 5
y - 6
x - 7
7-5 + 1 = 3
return str[5:7]
smallest = len(str)
smallest = max(pointer) - min(pointer)
pointer=[] index = same index as arr, that character is the one we're referring to
value = the position of that character arr[i] where we see it currently in str
"""