I am supposed to write a program in JavaScript to find all the anagrams within a series of words provided. e.g.: "monk, konm, nkom, bbc, cbb, dell, ledl, llde" The output should be categorised into rows: 1. monk konm, nkom; 2. bbc cbb; 3. dell ledl, llde;
I already sorted them into alphabetical order i.e.: "kmno kmno bbc bbc dell dell" and put them into an array.
However I am stuck in comparing and finding the matching anagram within the array.
Any help will be greatly appreciated.
Javascript objects are excellent for this purpose, since they are essentially key/value stores:
false
strA
e.g.Hello
-->{H: 1, e: 1, l: 2, o: 1}
false
, else set value to-1
Time complexity:
O(n)