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

猜你喜欢

转载自blog.csdn.net/m0_51210480/article/details/112059554
今日推荐