Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 14, 2016 05:55
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/adbfc2d809fb5b2bac78 to your computer and use it in GitHub Desktop.
Save jianminchen/adbfc2d809fb5b2bac78 to your computer and use it in GitHub Desktop.
Anagram algorithm - using string.Contains, Split functions
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string op = "";
int t = Convert.ToInt16(Console.ReadLine());
for (int q = 1; q <= t; q++)
{
string input = Console.ReadLine();
int count = 0;
if (input.Length % 2 == 1)
{
op += "-1\n";
}
else
{
string a = input.Substring(0, input.Length / 2);
string b = input.Substring(input.Length / 2, input.Length / 2);
string mn = "";
foreach (char x in b)
{
if (!mn.Contains(x))
{
mn += x;
}
}
foreach (char x in mn)
{
int one = a.Split(x).Length - 1;
int two = b.Split(x).Length - 1;
if (two > one)
{
count += two - one;
}
}
op += count.ToString() +"\n";
}
}
Console.WriteLine(op);
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment