哈希,记录字符串比较
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param s string字符串
# @param p string字符串
# @return int整型一维数组
#
from collections import Counter
class Solution:
def findWord(self , s: str, p: str) -> List[int]:
# write code here
def check(count_p, count_s):
if len(count_s) != len(count_p): return False
for k, p in count_p.items():
if k not in count_s or count_s[k] != p:
return False
return True
count_p = Counter(p)
res = []
for i in range(len(s) - len(p) + 1):
count_s = Counter(s[i:i + len(p)])
if check(count_p, count_s):
res.append(i)
return res