forked from rupashi97/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anagram.py
36 lines (24 loc) · 802 Bytes
/
anagram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'''
Find if t is a valid anagram of s
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase,
typically using all the original letters exactly once.
'''
import collections
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
htable = {}
if len(s) != len(t):
return False
for c in s:
htable[c] = htable[c] + 1 if c in htable else 1
for x in t:
if x in htable and htable[x] > 0:
htable[x] = htable[x] - 1
else:
return False
return True
class Solution2:
def isAnagram(self, s: str, t: str) -> bool:
tracker = collections.Counter(s)
for x in t: tracker[x] -= 1
return not any(tracker.values())