/// <summary> /// Using HashMapList/Dictionary /// </summary> /// <param name="array"></param> public static void Sort_GroupWordsByAnagram_UsingDictionary(string[] array) { Console.WriteLine(AssortedMethods.StringArrayToString(array)); Dictionary <string, LinkedList <string> > hash = new Dictionary <string, LinkedList <string> >(); /* Group words by anagram */ foreach (string s in array) { string key = SortChars(s); if (!hash.ContainsKey(key)) { hash.Add(key, new LinkedList <string>()); } LinkedList <string> anagrams = hash[key]; anagrams.AddLast(s); } /* Convert hash table to array */ int index = 0; foreach (string key in hash.Keys) { LinkedList <string> list = hash[key]; foreach (string t in list) { array[index] = t; index++; } } Console.WriteLine(AssortedMethods.StringArrayToString(array)); }
public override void Run() { string[] array = { "apple", "banana", "carrot", "ele", "duck", "papel", "tarroc", "cudk", "eel", "lee" }; Console.WriteLine(AssortedMethods.StringArrayToString(array)); Array.Sort(array, new AnagramComparator()); Console.WriteLine(AssortedMethods.StringArrayToString(array)); string[] array2 = { "apple", "banana", "carrot", "ele", "duck", "papel", "tarroc", "cudk", "eel", "lee" }; Sort(array); Console.WriteLine(AssortedMethods.StringArrayToString(array)); }
public static void Sort_GroupWordsByAnagram_UsingCustomAnagramComparator(string[] array) { Console.WriteLine(AssortedMethods.StringArrayToString(array)); Array.Sort(array, new AnagramComparator()); Console.WriteLine(AssortedMethods.StringArrayToString(array)); }