Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 14, 2016 06:56
Show Gist options
  • Save jianminchen/65687cefd2b107ec5e23 to your computer and use it in GitHub Desktop.
Save jianminchen/65687cefd2b107ec5e23 to your computer and use it in GitHub Desktop.
Anagram - string.Remove function - good idea
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
int T = Convert.ToInt32(Console.ReadLine());
string[] lines = new string[T];
for (int i = 0; i < T; i++)
{
lines[i] = Console.ReadLine();
}
for (int i = 0; i < T; i++)
{
if (lines[i].Length % 2 == 1) Console.WriteLine("-1");
else
{
string s1 = lines[i].Substring(0, lines[i].Length / 2);
string s2 = lines[i].Substring(lines[i].Length / 2);
int diff = 0;
for (int j = 0; j < s1.Length; j++)
{
if (s2.Contains(s1[j].ToString()))
{
s2 = s2.Remove(s2.IndexOf(s1[j]), 1);
}
else diff++;
}
Console.WriteLine(diff);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment