Skip to content

Instantly share code, notes, and snippets.

@guitarrapc guitarrapc/Anagram.cs
Last active Oct 3, 2016

Embed
What would you like to do?
Simple 2 string anagram check in C#
void Main()
{
Func<string, string, bool> anagram = (first, second) =>
{
if (first == null || second == null) return false;
if (first.Length != second.Length) return false;
return first.OrderBy(x => x).SequenceEqual(second.OrderBy(x => x));
};
Func<string, string, bool> anagram2 = (first, second) =>
{
if (first == null || second == null) return false;
if (first?.Length != second?.Length) return false;
return first?.OrderBy(x => x)?.SequenceEqual(second?.OrderBy(x => x)) ?? false; // ```?? false``` can be ```== true```
};
var sw = Stopwatch.StartNew();
foreach (var item in Enumerable.Range(0, 100000))
{
anagram(" ", " ");
anagram("1", "1");
anagram("eros", "rose");
anagram("eros", "lose");
anagram("", "rose");
anagram("eros", "rosesssss");
anagram("bab", "aba");
anagram(" ", " ");
anagram(null, null);
anagram(null, "");
anagram("hoge", null);
anagram("1", "2");
}
sw.Stop();
$"{sw.Elapsed.TotalMilliseconds}ms".Dump("Sequence");
Debug.Assert(anagram("", "").Dump());
Debug.Assert(anagram(" ", " ").Dump());
Debug.Assert(anagram("1", "1").Dump());
Debug.Assert(anagram("eros", "rose").Dump());
Debug.Assert(anagram("eros", "lose").Dump() == false);
Debug.Assert(anagram("", "rose").Dump() == false);
Debug.Assert(anagram("eros", "rosesssss").Dump() == false);
Debug.Assert(anagram("bab", "aba").Dump() == false);
Debug.Assert(anagram(" ", " ").Dump() == false);
Debug.Assert(anagram(null, null).Dump() == false);
Debug.Assert(anagram(null, "").Dump() == false);
Debug.Assert(anagram("hoge", null).Dump() == false);
Debug.Assert(anagram("1", "2").Dump() == false);
sw.Restart();
foreach (var item in Enumerable.Range(0, 100000))
{
anagram2(" ", " ");
anagram2("1", "1");
anagram2("eros", "rose");
anagram2("eros", "lose");
anagram2("", "rose");
anagram2("eros", "rosesssss");
anagram2("bab", "aba");
anagram2(" ", " ");
anagram2(null, null);
anagram2(null, "");
anagram2("hoge", null);
anagram2("1", "2");
}
sw.Stop();
$"{sw.Elapsed.TotalMilliseconds}ms".Dump("Sequence ?.");
Debug.Assert(anagram2("", "").Dump());
Debug.Assert(anagram2(" ", " ").Dump());
Debug.Assert(anagram2("1", "1").Dump());
Debug.Assert(anagram2("eros", "rose").Dump());
Debug.Assert(anagram2("eros", "lose").Dump() == false);
Debug.Assert(anagram2("", "rose").Dump() == false);
Debug.Assert(anagram2("eros", "rosesssss").Dump() == false);
Debug.Assert(anagram2("bab", "aba").Dump() == false);
Debug.Assert(anagram2(" ", " ").Dump() == false);
Debug.Assert(anagram2(null, null).Dump() == false);
Debug.Assert(anagram2(null, "").Dump() == false);
Debug.Assert(anagram2("hoge", null).Dump() == false);
Debug.Assert(anagram2("1", "2").Dump() == false);
}
@guitarrapc

This comment has been minimized.

Copy link
Owner Author

commented Oct 3, 2016

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.