leetcode-242 | 有效的字母异位词


title: leetcode-242 | 有效的字母异位词
date: 2019-4-25 17:01:07
comments: true
categories: “leetcode”
tags:
- leetcode 简单难度

242. 有效的字母异位词(Valid Anagram)

给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词。

示例1:
输入: s = “anagram”, t = “nagaram”
输出: true
示例2:
输入: s = “rat”, t = “car”
输出: false
说明:
你可以假设字符串只包含小写字母。

进阶:
如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况?

思路

方法一: 数据装入字典中,两个字符串对应的字典比较

长度不等,直接返回False

class Solution(object):
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        s_len, t_len = len(s), len(t)
        if s_len!=t_len:
            return False
        s_dict = dict()
        for i in s:
            s_dict[i] = s_dict.get(i,0) + 1
        t_dict = dict()
        for j in t:
            t_dict[j] = t_dict.get(j, 0) + 1

        if s_dict!=t_dict:
            return False
        return True 

结果:
执行用时 : 100 ms, 在Valid Anagram的Python提交中击败了17.09% 的用户
内存消耗 : 12.5 MB, 在Valid Anagram的Python提交中击败了43.88% 的用户

提交时间 状态 执行用时 内存消耗 语言
几秒前 通过 100 ms 12.5MB python

方法二:观察特色

如果两个字符串相等,那么我们对应的所有的字符集合应该是一样的。
这里我们使用count函数来统计。

class Solution(object):
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        s_len, t_len = len(s), len(t)
        if s_len!=t_len:
            return False
        se = set(s)
        for i in se:
            if s.count(i)!=t.count(i):
                return False
        return True 

结果:
执行用时 : 36 ms, 在Valid Anagram的Python提交中击败了99.50% 的用户
内存消耗 : 12.8 MB, 在Valid Anagram的Python提交中击败了29.64% 的用户

提交时间 状态 执行用时 内存消耗 语言
几秒前 通过 36 ms 12.8MB python
发布了169 篇原创文章 · 获赞 139 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_26460841/article/details/102641695