Create a gist now

Instantly share code, notes, and snippets.

will be fail in bab == aba
void Main()
{
Func<string, string, bool> anagram = (first, second) =>
{
if (string.IsNullOrEmpty(first) || string.IsNullOrEmpty(second)) return false;
if (first.Length != second.Length) return false;
return !first.Except(second).Any();
};
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");
"".Dump("Sort");
Debug.Assert(anagram(" ", " ").Dump("1"));
Debug.Assert(anagram("1", "1").Dump("2"));
Debug.Assert(anagram("eros", "rose").Dump("3"));
Debug.Assert(anagram("eros", "lose").Dump("4") == false);
Debug.Assert(anagram("", "rose").Dump("5") == false);
Debug.Assert(anagram("eros", "rosesssss").Dump("6") == false);
Debug.Assert(anagram("bab", "aba").Dump("7") == false);
Debug.Assert(anagram(" ", " ").Dump("8") == false);
Debug.Assert(anagram(null, null).Dump("9") == false);
Debug.Assert(anagram(null, "").Dump("10") == false);
Debug.Assert(anagram("hoge", null).Dump("11") == false);
Debug.Assert(anagram("1", "2").Dump("12") == false);
}
@guitarrapc
Owner
guitarrapc commented Oct 3, 2016 edited

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment