Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 14, 2016 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/7bbe86bbb83787d6b98b to your computer and use it in GitHub Desktop.
Save jianminchen/7bbe86bbb83787d6b98b to your computer and use it in GitHub Desktop.
Anagram - add all the difference between two strings, and then divide it by 2
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static int Solve(string s)
{
if(s.Length % 2 == 1)
return -1;
int [] count = new int[26];
for(int i = 0; i < s.Length /2; ++i)
{
count[(int)s[i] - (int)'a'] += 1;
count[(int)s[s.Length -1 - i] - (int)'a'] -= 1;
}
int res = 0;
foreach(int i in count)
{
res += Math.Abs(i);
}
return res/2;
}
static void Main(String[] args) {
int t = Convert.ToInt32(Console.ReadLine());
for(int i = 0; i< t; ++i)
{
Console.WriteLine(Solve(Console.ReadLine()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment