Hacker Rank Solutions: Anagram
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
| tests = $stdin.gets.to_i
tests.times do
string = $stdin.gets.chomp
if string.length % 2 == 0
middle = string.length / 2
left = string[0..(middle - 1)]
right = string[middle..(string.length - 1)]
left_sort = left.chars.sort
right_sort = right.chars.sort
if left_sort == right_sort
return 0
else
left_sort.each do |x|
index = right_sort.index(x)
right_sort.delete_at(index) unless index.nil?
end
puts right_sort.length
end
else
puts -1
end
end
|