Skip to content

Instantly share code, notes, and snippets.

@letscodego
Created August 13, 2022 06:42
Show Gist options
  • Save letscodego/ba1bd8e71a91c4361d6d6f9881caef52 to your computer and use it in GitHub Desktop.
Save letscodego/ba1bd8e71a91c4361d6d6f9881caef52 to your computer and use it in GitHub Desktop.
public static bool IsAnagram(string s, string t)
{
if (s.Length != t.Length)
return false;
var s_hash = new Hashtable();
var t_hash = new Hashtable();
for (var i = 0; i < s.Length; i++)
{
s_hash[s.Substring(i, 1)] = 1 +
(int)(s_hash.ContainsKey(s.Substring(i, 1)) ?
s_hash[s.Substring(i, 1)] : 0);
t_hash[t.Substring(i, 1)] = 1 +
(int)(t_hash.ContainsKey(t.Substring(i, 1)) ?
t_hash[t.Substring(i, 1)] : 0);
}
foreach (DictionaryEntry d in s_hash)
{
if (!t_hash.ContainsKey(d.Key))
return false;
else
if ((int)d.Value != (int)t_hash[d.Key])
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment