Minimum string array coverage
Description
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 <= 50Example
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.Given tag_list =["a"], all_tags =["b"], return-1.
Explanation:
Does not exist.分析
夹逼法,此题假设target里的没有duplicate,2个Map做
先m1里存所有target和count,m2用来后来增减target
右边for loop前进,如果遇到target就存入m2,直到存满所有target:m2.size() = target.size(),就可以开始减少left开始夹逼
夹逼的while里,一定要记得判断left也是target才能进入m2的操作
结尾记得判断如果长度就是all tags,就是不存在,返回-1
用的模板,注意最后条件 如果d=n+1的话 要返回-1。因为初始化d=n+1.
如果初始化时 float("inf"),就比较d == float("inf") return -1
Last updated
Was this helpful?