IsAnagramEdit
If two words contain the same characters they’re anagrams.
There are two solutions:
- Sort the characters using the lexicographic order and find out whether all the characters in one string are equal to other string and are in the same order.
Sorting strings - complexity O(nlogn)
bool IsAnagram(string word1, string word2)
{
char[] s1 = word1.ToCharArray();
char[] s2 = word2.ToCharArray();
Array.Sort(s1);
Array.Sort(s2);
string val1 = new string(s1);
string val2 = new string(s2);
if (val1 == val2)
{
Console.WriteLine("This is Anagram");
return true;
}
else
{
Console.WriteLine("This is not Anagram");
return false;
}
}
- Hashmap approach where key is the letter and value is the frequency. Only if frequency of all letters is 0 the strings are an anagram.
static bool IsAnagram(string word1, string word2)
{
if (word1.Length != word2.Length)
{
return false;
}
var symbolFrequency = new Dictionary<char, int>();
for (int i = 0; i < word1.Length; i++)
{
symbolFrequency.TryAdd(word1[i], 0);
symbolFrequency.TryAdd(word2[i], 0);
symbolFrequency[word1[i]]++;
symbolFrequency[word2[i]]--;
}
return symbolFrequency.Values.All(frequence => frequence == 0);
}
Console.WriteLine(IsAnagram("asd", "dsa"));