Leetcode每日一题2020.11.22第242题:有效的字母异位词

题目描述

在这里插入图片描述

示例

在这里插入图片描述

思路、算法及代码实现

题目要求可以简化为:字符串 s 和 t 中每个字母出现的次数一致时,返回 True ,否则,返回False。本题难度为简单,如果用的是Python的话,可以直接调用 collections 模块的 Counter 方法,只需一行代码即可。

import collections

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        return collections.Counter(s) == collections.Counter(t)

在这里插入图片描述

小知识点:collections.Counter()方法

Counter中文意思是计数器,也就是我们常用于统计的一种数据类型。

统计词频
from collections import Counter
colors = ['red','blue','red','green','blue','blue']
c = Counter(colors)
print(dict(c))
# {'red': 2, 'blue': 3, 'green': 1}

更多用法见博客:https://blog.csdn.net/qwe1257/article/details/83272340

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值