forked from Srinivas11789/AlgorithmNuggets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanagram.py
More file actions
48 lines (35 loc) · 895 Bytes
/
anagram.py
File metadata and controls
48 lines (35 loc) · 895 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/python
import sys
def anagaram(s):
# convert string to list and length
s = list(s)
n = len(s)
# odd condition
if n%2 != 0:
return -1
# even condition - split
mid = n//2
left = s[:mid]
right = s[mid:]
# O(N)
# remove all similar elements from the array
# remaining is the one to be changed!
for i in range(len(left)):
if left[i] in right:
right.pop(right.index(left[i]))
return len(right)
"""
# only a few passes O(N2)
count = 0
for i in range(len(left)):
for j in range(len(right)):
if left[i] == right[j]:
count += 1
break
return len(left) - count
"""
q = int(raw_input().strip())
for a0 in xrange(q):
s = raw_input().strip()
result = anagaram(s)
print(result)