From 3f45b56cef522ca54b648ded97a8d3b0c6a0f919 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:47:01 +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 | 68 +---------------------- 1 file changed, 1 insertion(+), 67 deletions(-) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 0e5683c7..3f4b00dc 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -122,21 +122,7 @@ class Solution { ``` Python: -(版本一) 使用数组 -```python -class Solution: - def isAnagram(self, s: str, t: str) -> bool: - record = [0] * 26 - for i in range(len(s)): - record[ord(s[i]) - ord("a")] += 1 - for i in range(len(t)): - record[ord(t[i]) - ord("a")] -= 1 - for i in range(26): - if record[i] != 0: - return False - return True -``` -(版本二) 使用数组 + ```python class Solution: def isAnagram(self, s: str, t: str) -> bool: @@ -153,59 +139,7 @@ class Solution: return True ``` -(版本三) 使用defaultdict -```python -from collections import defaultdict - -class Solution: - def isAnagram(self, s: str, t: str) -> bool: - - 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 -class Solution: - def isAnagram(self, s: str, t: str) -> bool: - if len(s) != len(t): - return False - - hash_table_s = {} - hash_table_t = {} - - for i in range(len(s)): - hash_table_s[s[i]] = hash_table_s.get(s[i], 0) + 1 - hash_table_t[t[i]] = hash_table_t.get(t[i], 0) + 1 - - return hash_table_s == hash_table_t -``` - -(版本五) 使用排序 - -```python -class Solution: - def isAnagram(self, s: str, t: str) -> bool: - return sorted(s) == sorted(t) -``` -(版本六) 使用Counter - -```python -from collections import Counter - -class Solution: - def isAnagram(self, s: str, t: str) -> bool: - return Counter(s) == Counter(t) -``` Go: ```go