Created
March 14, 2016 06:52
-
-
Save jianminchen/d972656068fa8088ae70 to your computer and use it in GitHub Desktop.
Anagram - StringBuilder, appendLine function, and interesting to read.
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; | |
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