leetcode
程序员文章站
2022-04-27 12:17:27
...
leetcode 30
看了题解,第一次使用collections 这个包
collection.Counter()输入一个列表,可以返回哈希表
那其实这个题就可以判断哈希表相等不相等来比较了。因为如果直接比较元素的话会出现一个问题就是
因为要删除出去一个元素在进行下一次的比较,滑动到‘babaab’时,删除‘ab’,这时候就会删错。所以用这个哈希表来进行比较
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
if (s is '') or (words == []):
return []
one_len=len(words[0])
sum_len=len(words)*one_len
hash_map1=Counter(words)
hash_map2=[]
res=[]
for i in range(len(s)-sum_len+1):
hash_map2=[]
curr=s[i:i+sum_len]
for j in range(len(words)):
hash_map2.append(curr[j*one_len:j*one_len+one_len])
if Counter(hash_map2)==hash_map1:
res.append(i)
return res
上一篇: eclipse怎么调成中文版