From 6e88b2034d70d261a9cd0388cdcf906e50677195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Sun, 7 May 2023 09:54:15 +0800 Subject: [PATCH] =?UTF-8?q?Update=200242.=E6=9C=89=E6=95=88=E7=9A=84?= =?UTF-8?q?=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0242.有效的字母异位词.md | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 3f4b00dc..09674ecd 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -138,7 +138,31 @@ class Solution: return False return True ``` +Python写法二(没有使用数组作为哈希表,只是介绍defaultdict这样一种解题思路): +```python +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + from collections import defaultdict + + s_dict = defaultdict(int) + t_dict = defaultdict(int) + for x in s: + s_dict[x] += 1 + + for x in t: + t_dict[x] += 1 + return s_dict == t_dict +``` +Python写法三(没有使用数组作为哈希表,只是介绍Counter这种更方便的解题思路): + +```python +class Solution(object): + def isAnagram(self, s: str, t: str) -> bool: + from collections import Counter + a_count = Counter(s) + b_count = Counter(t) + return a_count == b_count Go: