-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmallestUniqueSubstring.py
134 lines (109 loc) · 3.11 KB
/
smallestUniqueSubstring.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
'''
01234567
xyyzyzyx
iteration 1:
Step 1:
identify substring
x
xy
xyyz
beg = 0
end = 4
step2 :
remove unnece.. chars
iteration 2:
step 1
beg = 1
y
yy
yyz
yyzy
yyzyz
yyzyzy
yyzyzyx
step2:
beg = 1
yzyzyx [y:3 z:2 x:1]
zyzyx
yzyx
zyx
beg = 5
end = 7
beg, end
trackerMap{y: .. z : .. x: ..} = arr o(1)
while all chars exhausted:
Problem statement::::
Smallest Substring of All Characters
Given an array of unique characters arr and a string str, Implement a function getShortestUniqueSubstring that finds the smallest substring of str containing all the characters in arr. Return "" (empty string) if such a substring doesn’t exist.
Come up with an asymptotically optimal solution and analyze the time and space complexities.
Example:
input: arr = ['x','y','z'], str = "xyyzyzyx"
output: "zyx"
Constraints:
[time limit] 5000ms
[input] array.character arr
1 = arr.length = 30
[input] string str
1 = str.length = 500
[output] string
Took an eternity to solve this!!!
'''
# [x,y,z] "xyzx" -> xyz, yzx
# abcx -> ""
# xyz -> xayz
# xyysszyx
# minCount = 4
# 0 : xyyz > map {0 : 4}
# 1 : yzyx > map {1: 4}
# 2 : x
# arr = [x,y,z], str = "xxxyxxaz"
# 0 : 1: 2:x 3:x 4: 5: map {2:0}arr = [yz]< 3
# xyxxyxxaz yxxaz
# 12314501
# xyxyaz -> beg = 0, end = 0
# end = 1, sub = "xy", end = 2, "xyx", end = 3, "xyxy", end = 4, xyxya, end = 5, xyxyaz
# beg = 1, beg = 2
# xyaz
def get_shortest_unique_substring(arr, str):
head = 0 # begin pointer
result = "" # holds the substring
counter = 0 # keeps track of uniqueness of char in subsequence
countMap = {} # map to hold subsequencing char count
# initialize countMap get count of all possible chars in arr
for item in arr:
countMap[item] = 0
# scan string character by character
for tail in range(len(str)):
# set end pointer
lastItem = str[tail]
# skip all the characters which are not in arr
if not(lastItem in countMap):
continue
# get how many time we should see the character
endCount = countMap[lastItem]
# if we have seen char before then increase counter, Unique counter
if endCount == 0:
counter += 1
# keep track of how many times char is encountered
countMap[lastItem]+= 1
# push begin forward
while counter == len(arr):
subLen = tail - head + 1
if subLen == len(arr):
# return a substring of str from
# head to tail
return str[head: tail+1]
if result == "" or subLen < len(result):
# return a substring of str from
# head to tail
result = str[head: tail+1]
#begin index
firstItem = str[head]
# remove non-unique items, from head of subsequence
if firstItem in countMap:
beginCount = countMap[firstItem] - 1
if beginCount == 0:
counter = counter - 1
countMap[firstItem]= beginCount
head += 1
return result