Skip to content

Commit 50a1a9d

Browse files
authored
Create anagram.py
1 parent 5284471 commit 50a1a9d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

anagram.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# https://www.hackerrank.com/challenges/anagram/problem
2+
3+
# old logic.
4+
5+
#!/bin/python3
6+
7+
import sys
8+
from collections import defaultdict
9+
10+
11+
def anagram(s):
12+
size = len(s)
13+
if size % 2 == 1:
14+
return -1
15+
mydict = defaultdict(int)
16+
for i in range(size//2):
17+
item = s[i]
18+
mydict[item]+=1
19+
for i in range(size//2,size):
20+
item = s[i]
21+
mydict[item]-=1
22+
count = 0
23+
for item in mydict.values():
24+
count += abs(item)
25+
return count//2
26+
27+
28+
29+
30+
q = int(input().strip())
31+
for a0 in range(q):
32+
s = input().strip()
33+
result = anagram(s)
34+
print(result)
35+
36+
37+

0 commit comments

Comments
 (0)