-
Notifications
You must be signed in to change notification settings - Fork 8
/
isAnagram.py
36 lines (32 loc) · 1.03 KB
/
isAnagram.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
def isAnagram(self, s: str, t: str) -> bool:
if len(s) !=len(t):
return False
s_history ={}
for ch in s:
if ch in s_history:
s_history[ch]+=1
else:
s_history[ch]=1
t_history ={}
for ch in t:
if ch in t_history:
t_history[ch]+=1
else:
t_history[ch]=1
for item in s_history:
if item in t_history and s_history[item]==t_history[item]:
continue
else:
return False
return True
def isAnagram(original, anagram, firstPtr=0, secondPtr=0):
if len(original) != len(anagram):
return False
if firstPtr == len(original):
return True
if secondPtr == len(anagram) :
return False
if original[firstPtr] == anagram[secondPtr]:
return isAnagram(original, anagram, firstPtr+1, 0)
else:
return isAnagram(original, anagram, firstPtr, secondPtr+1)