-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
王洋
committed
Jan 18, 2024
1 parent
2e3068c
commit 1488802
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
class Solution: | ||
def isAnagram(self, s: str, t: str) -> bool: | ||
# 判断长度 | ||
if len(s) != len(t): | ||
return False | ||
|
||
mapS: dict = {} | ||
mapT: dict = {} | ||
for i in range(len(s)): | ||
if s[i] in mapS: | ||
mapS[s[i]] = mapS[s[i]] + 1 | ||
else: | ||
mapS[s[i]] = 1 | ||
|
||
if t[i] in mapT: | ||
mapT[t[i]] = mapT[t[i]] + 1 | ||
else: | ||
mapT[t[i]] = 1 | ||
|
||
if len(mapS) != len(mapT): | ||
return False | ||
|
||
for key in mapS.keys(): | ||
if not (key in mapT) or (mapS[key] != mapT[key]): | ||
return False | ||
# if mapS[key] != mapT[key]: | ||
# return False | ||
# if (key in mapT) and (mapS[key] != mapT[key]): | ||
# return False | ||
return True | ||
|
||
|
||
s = Solution() | ||
# print(s.isAnagram('anagram','nagaram')) | ||
print(s.isAnagram('rat','car')) | ||
|
||
# a: dict = {} | ||
# a['a'] = 1 | ||
# a['b'] = 2 | ||
# a['c'] = 3 | ||
|
||
# print(a['a']) |