mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #394 from borninfreedom/master
update有效的字母异位词,python代码添加了更pythonic的写法
This commit is contained in:
@ -130,7 +130,27 @@ class Solution:
|
|||||||
return True
|
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
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func isAnagram(s string, t string) bool {
|
func isAnagram(s string, t string) bool {
|
||||||
if len(s)!=len(t){
|
if len(s)!=len(t){
|
||||||
|
Reference in New Issue
Block a user