Given a string collection tag_list, an array of strings all_tags, find the smallest all_tags sub-array contains all the string in the tag_list, output the length of this sub-array. If there is no return-1.
1 <= |tag_list|,|all_tags| <=10000
All string length <= 50
Example
Given tag_list =["made","in","china"], all_tags =["made", "a","b","c","in", "china","made","b","c","d"], return3.
Explanation:
["in", "china","made"] contains all the strings in tag_list,6 - 4 + 1 = 3.
import collections
class Solution:
"""
@param tagList: The tag list.
@param allTags: All the tags.
@return: Return the answer
"""
def getMinimumStringArray(self, tagList, allTags):
# Write your code here
map = collections.defaultdict(int)
for i in tagList:
map[i] += 1
start=end=0
counter=len(tagList)
n = len(allTags)
d=n+1
while end<n:
if map[allTags[end]]>0:
counter -=1
map[allTags[end]] -= 1
end +=1
while counter == 0:
d = min(d,end-start)
if map[allTags[start]] == 0:
counter += 1
map[allTags[start]] += 1
start += 1
if d == n+1:
return -1
return d