Skip to content

Commit c2a9351

Browse files
committed
Update is_anagram.py to take both upper and lowercase letters
1 parent e6140b3 commit c2a9351

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

String/Anagram/is_anagram.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
def is_anagram(a: str, b: str):
2-
a, b = a.lower(), b.lower()
32
count = [0 for _ in range(26)]
3+
count2 = [0 for _ in range(26)]
44
a_length, b_length = len(a), len(b)
55

66
if a_length != b_length:
@@ -9,10 +9,14 @@ def is_anagram(a: str, b: str):
99
for i in range(a_length):
1010
if 'a' <= a[i] <= 'z':
1111
count[ord(a[i]) - ord('a')] += 1
12+
elif 'A' <= a[i] <= 'Z':
13+
count2[ord(a[i]) - ord('A')] += 1
1214
if 'a' <= b[i] <= 'z':
1315
count[ord(b[i]) - ord('a')] -= 1
16+
elif 'A' <= b[i] <= 'Z':
17+
count2[ord(b[i]) - ord('A')] -= 1
1418

15-
if any(count):
19+
if any(count) or any(count2):
1620
return False
1721

1822
return True

0 commit comments

Comments
 (0)