-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAnagram.py
More file actions
59 lines (42 loc) · 1.22 KB
/
Copy pathAnagram.py
File metadata and controls
59 lines (42 loc) · 1.22 KB
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
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'anagram' function below.
#
# The function is expected to return an INTEGER.
# The function accepts STRING s as parameter.
#
def anagram(s):
# Write your code here
# Check if the length of the string is odd, as it's not possible to split into two equal substrings
if len(s) % 2 != 0:
return -1
# Split the string into two equal substrings
mid = len(s) // 2
s1 = s[:mid]
s2 = s[mid:]
# Count the occurrences of each character in both substrings
count_s1 = [0] * 26
count_s2 = [0] * 26
for char in s1:
count_s1[ord(char) - ord('a')] += 1
for char in s2:
count_s2[ord(char) - ord('a')] += 1
# Calculate the minimum number of changes needed to make the substrings anagrams
changes_needed = 0
for i in range(26):
changes_needed += abs(count_s1[i] - count_s2[i])
# Return the result
return changes_needed // 2
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
q = int(input().strip())
for q_itr in range(q):
s = input()
result = anagram(s)
fptr.write(str(result) + '\n')
fptr.close()