Created
March 14, 2016 06:02
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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