Created
March 14, 2016 05:55
-
-
Save jianminchen/adbfc2d809fb5b2bac78 to your computer and use it in GitHub Desktop.
Anagram algorithm - using string.Contains, Split functions
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.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