Skip to content

Commit

Permalink
✨ feat: python解法
Browse files Browse the repository at this point in the history
  • Loading branch information
王洋 committed Jan 18, 2024
1 parent 2e3068c commit 1488802
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
42 changes: 42 additions & 0 deletions problemset/valid-anagram/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,46 @@ export function isAnagram1(s: string, t: string): boolean {

```python

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'])
```
43 changes: 43 additions & 0 deletions problemset/valid-anagram/index.py
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'])

0 comments on commit 1488802

Please sign in to comment.