Skip to content

Instantly share code, notes, and snippets.

@m1el
Created December 19, 2013 11:02
Show Gist options
  • Save m1el/8037592 to your computer and use it in GitHub Desktop.
Save m1el/8037592 to your computer and use it in GitHub Desktop.
anagram c# tester
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static bool IsAnagram(string word1, string word2) {
if (word1.Length != word2.Length) {
return false;
}
var dict1 = CountChars(word1);
var dict2 = CountChars(word2);
return dict1.Count == dict2.Count &&
Array.TrueForAll(dict1.Keys.ToArray(),
k => dict2.ContainsKey(k) && (dict1[k] == dict2[k]));
}
static Dictionary<char, int> CountChars(string word) {
var dict = new Dictionary<char, int>();
foreach (var k in word.ToCharArray()) {
dict[k] = dict.ContainsKey(k) ? dict[k] + 1 : 1;
}
return dict;
}
static void Main(string[] args)
{
var cargs = Environment.GetCommandLineArgs();
if (cargs.Length < 3) {
Console.WriteLine("Not enough arguments!");
Environment.Exit(1);
}
Console.WriteLine(IsAnagram(cargs[1], cargs[2]));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment