LeetCode 242 Valid Anagram 解题报告

题目要求

Given two strings s and , write a function to determine if t is an anagram of s.

题目分析及思路

给出两个字符串s和t,要求判断t是否是s的一个anagram,即由相同的字母且不同的字母顺序组成。可以使用collections.Counter统计每个字符串中不同字母的的个数,若两个字符串中的字母种类和个数都相等,则可判定t是s的一个anagram。

python代码

class Solution:

    def isAnagram(self, s: str, t: str) -> bool:

        c1 = collections.Counter(s)

        c2 = collections.Counter(t)

        if c1 == c2:

            return True

        else:

            return False

        

猜你喜欢

转载自www.cnblogs.com/yao1996/p/10703971.html
今日推荐