Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 14, 2016 06:52
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/d972656068fa8088ae70 to your computer and use it in GitHub Desktop.
Save jianminchen/d972656068fa8088ae70 to your computer and use it in GitHub Desktop.
Anagram - StringBuilder, appendLine function, and interesting to read.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
class Solution {
static void Main(String[] args)
{
StringBuilder results = new StringBuilder();
int testsCount = int.Parse(Console.ReadLine());
for(int i=0; i<testsCount; i++)
{
string word = Console.ReadLine();
if (word.Length % 2 != 0)
{
results.AppendLine("-1");
continue;
}
char[] a = word.Substring(0, word.Length/2).ToCharArray();
char[] b = word.Substring(word.Length/2, word.Length - word.Length/2).ToCharArray();
int moves = 0;
for(int j=0; j<a.Length; j++)
{
int index = FirstIndexOf(b, a[j]);
if (index == -1)
moves++;
else
b[index] = ' ';
}
results.AppendLine(moves.ToString());
}
Console.WriteLine(results.ToString());
}
private static int FirstIndexOf(char[] arr, char x)
{
for(int i=0; i<arr.Length; i++)
{
if (arr[i] == x)
return i;
}
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment