题解 | #找到字符串中的异位词#

找到字符串中的异位词

http://www.nowcoder.com/practice/9ff491c910e5427fab6ae67745929085

哈希,记录字符串比较

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @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
全部评论

相关推荐

10-28 10:48
已编辑
门头沟学院 Java
孩子我想要offer:发笔试后还没笔试把我挂了,然后邮箱一直让我测评没测,后面不知道干嘛又给我捞起来下轮笔试,做完测评笔试又挂了😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务