In a clash code I was proposed to test whether two words are anagrams or not. Anagrams are words with the same letters, ignoring the case, I presume in the same numbers, but not necessarily in the same order. It was supposed that both words were already verified in a dictionary.
As a practical protocol, two words separated by a space are received on an input line, and the program must display 1 if the two words are anagrams, 0 if not.
On the first approach I used two Dictionary<char, int>, begun by initializing them with all letters of the alphabet, and then increased with each letter found in the words.
After that, I realized it is possible to make a three times lighter program.
Perhaps somebody will make even better ?
I just give you the contents of my main() function, only System appears in the references.
string inputs = Console.ReadLine().ToLower().Split(' '); string mot1 = inputs; string mot2 = inputs; char c1 = mot1.ToCharArray(); char c2 = mot2.ToCharArray(); Array.Sort<char>(c1);Array.Sort<char>(c2); Console.WriteLine(new string(c1) == new string(c2) ? "1" : "0");