forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathis_anagram.py
More file actions
30 lines (24 loc) · 752 Bytes
/
is_anagram.py
File metadata and controls
30 lines (24 loc) · 752 Bytes
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
def is_anagram(a: str, b: str):
count = [0 for _ in range(26)]
count2 = [0 for _ in range(26)]
a_length, b_length = len(a), len(b)
if a_length != b_length:
return False
for i in range(a_length):
if 'a' <= a[i] <= 'z':
count[ord(a[i]) - ord('a')] += 1
elif 'A' <= a[i] <= 'Z':
count2[ord(a[i]) - ord('A')] += 1
if 'a' <= b[i] <= 'z':
count[ord(b[i]) - ord('a')] -= 1
elif 'A' <= b[i] <= 'Z':
count2[ord(b[i]) - ord('A')] -= 1
if any(count) or any(count2):
return False
return True
if __name__ == '__main__':
a, b = 'Abc', 'cab'
if is_anagram(a, b):
print('Anagrams')
else:
print('Not Anagrams')