代码随想录-哈希表01-python解法

242.有效的字母异位词

题目链接

采用Counter函数简单实现

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        # from collections import Counter
        sc = Counter(s)
        tc = Counter(t)
        return sc == tc
  • 383. 赎金信

题目链接

Counter解法与上题类似,最后比较时需要改成Counter(ransomNote) <= Counter(magazine),因为题目限定小写英文字母,也可用数组构建哈希表方法

class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        count = [0] * 26
        for s in magazine:
            count[ord(s) - ord("a")] += 1
        for s in ransomNote:
            count[ord(s) - ord("a")] -= 1
            if count[ord(s) - ord("a")] < 0:
                return False
        return True
  • 49.字母异位词分组

题目链接

由于互为字母异位词的两个字符串包含的字母相同,因此对两个字符串分别进行排序之后得到的字符串一定是相同的,故可以将排序之后的字符串作为哈希表的键

class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        tmp = collections.defaultdict(list)
        for st in strs:
            key = "".join(sorted(st))
            tmp[key].append(st)
        return list(tmp.values())
  • 438.找到字符串中所有字母异位词 

题目链接

用滑动窗口法,重点在于记录窗口内的字母情况,随着窗口滑动改变,优化的滑动窗口法理解有点困难,容易出错

class Solution:
    def findAnagrams(self, s: str, p: str) -> List[int]:
        #滑动窗口法
        lenp = len(p)
        lens = len(s)
        out = []
        if lenp > lens:
            return out
        countp = [0] * 26
        counts = [0] * 26
        for ind in range(lenp):
            countp[ord(p[ind]) - ord('a')] += 1
            counts[ord(s[ind]) - ord('a')] += 1
        if countp == counts:
            out.append(0)
        for ind in range(lens-lenp):
            #减去左端ind值
            counts[ord(s[ind]) - ord('a')] -= 1
            #加上右端值
            counts[ord(s[ind + lenp]) - ord('a')] += 1
            if countp == counts:
                out.append(ind+1)
        return out
class Solution:
    def findAnagrams(self, s: str, p: str) -> List[int]:
        #优化滑动窗口法
        lenp = len(p)
        lens = len(s)
        out = []
        if lenp > lens:
            return out
        countd = [0] * 26
        differ = 0
        for ind in range(lenp):
            countd[ord(s[ind]) - ord('a')] += 1
            countd[ord(p[ind]) - ord('a')] -= 1
        differ = [c != 0 for c in countd].count(True)
        if differ == 0:
            out.append(0)
        for ind in range(lens-lenp):
            #减去左端ind值
            countd[ord(s[ind]) - ord('a')] -= 1
            if countd[ord(s[ind]) - ord('a')] == 0:
                differ -= 1
            elif countd[ord(s[ind]) - ord('a')] == -1:
                differ += 1
            #加上右端值
            countd[ord(s[ind + lenp]) - ord('a')] += 1
            if countd[ord(s[ind + lenp]) - ord('a')] == 0:
                differ -= 1
            elif countd[ord(s[ind + lenp]) - ord('a')] == 1:
                differ += 1 
            if differ == 0:
                out.append(ind+1)
        return out

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值