# Python刷leetcode--242.有效的字母异位词

## 解法

### 解法一：

class Solution:
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
res = {}
for i in s:
if i in res:
res[i] += 1
else:
res[i] = 1
for i in t:
if i not in res:
return False
else:
res[i] -= 1
for i in res:
if res[i] != 0:
return False
return True


### 解法二：[思路一样，更加优雅]

import collections

class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
if len(s) != len(t):
return False
dicts = collections.defaultdict(int)
for i in range(len(s)):
dicts[s[i]] = dicts[s[i]] + 1
dicts[t[i]] = dicts[t[i]] - 1
for i in dicts.values():
if i != 0:
return False
return True


### 解法三： [排序一样对比]

class Solution(object):
def isAnagram(self, s, t):
if len(s) != len(t):
return False
if sorted(s) != sorted(t):
return False
return True


## 收获

### 一、defaultdict[修改默认返回值]

from collections import defaultdict

dict1 = defaultdict(int)
dict2 = defaultdict(set)
dict3 = defaultdict(str)
dict4 = defaultdict(list)
dict1[2] ='two'

print(dict1[1])
print(dict2[1])
print(dict3[1])
print(dict4[1])
'''

'''


### 二、python按照长度遍历

for i in range(len(s))


### 三、dict的用法

# 遍历键
for i in dicts:
# 遍历值
for i in dicts.values():


https://blog.csdn.net/qq_42873554/article/details/106827677

03-27 255

04-23 119

09-10 181

06-05 499

08-21 57

04-23 886

01-07 44

03-27 432

03-17 845

10-14 250

#### LeetCode python-242. 有效的字母异位词

©️2020 CSDN 皮肤主题: 像素格子 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。