-
Notifications
You must be signed in to change notification settings - Fork 0
/
valid_anagram.py
68 lines (58 loc) · 1.65 KB
/
valid_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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def isAnagram(s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
"""
#? The below solution maybe faster, but it depends on the
#? in-built sort function, where the time and space complexity
#? could be more than expected. Sometimes interviewers take space
#? as O(1) for in-built. So better to write own sorting algo and work it.
return(sorted(s) == sorted(t)) #!NEETCODE #1 """
if(len(s) != len(t)): #! NEETCODE #2
return False
#! return(Counter(s) == Counter(t))
#? The whole code below can be done in one line,
#? which is written above.
countS, countT = {}, {}
for i in range(len(s)):
countS[s[i]] = 1 + countS.get(s[i], 0)
countT[t[i]] = 1 + countT.get(t[i], 0)
for c in countS:
if countS[c] != countT.get(c, 0):
return False
return True
""" if(len(s) != len(t)): #! MY CODE
return False
for post in range(len(t)):
if t[post] not in s:
return False
s = s.replace('t[post]', '')
return True """
"""
if len(s) != len(t): #!LEETCODE fastest code, good if condts to end early but still
#! similar to NEETCODE
return False
cs = {}
for c in s:
if c not in cs:
cs[c] = 1
else:
cs[c] += 1
for c in t:
if c not in cs:
return False
else:
cs[c] -= 1
if cs[c] == 0:
cs.pop(c)
if(len(cs) > 0):
return False
return True """
s = 'ccac'
t = 'aacc'
if isAnagram(t,s):
print("TRUE")
else:
print("FALSE")