-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGroupAnagrams.js
55 lines (52 loc) · 1.6 KB
/
GroupAnagrams.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* @author Anirudh Sharma
*
* Given an array of strings, return all groups of strings that are anagrams.
* The groups must be created in order of their appearance in the original array.
*
* Constraints:
* 1<=N<=100
*/
const groupAnagrams = (words) => {
// Copy all the words in the array to a list
const wordList = [];
for (let word of words) {
wordList.push(word);
}
// Sort these words on the list alphabetically
for (let i = 0; i < wordList.length; i++) {
// Sort this word and store it in the list
wordList[i] = wordList[i].split("").sort().join("");
}
// Map to store a word and its corresponding indices
const anagrams = new Map();
// Loop through the words array
for (let i = 0; i < words.length; i++) {
if (anagrams.has(wordList[i])) {
anagrams.get(wordList[i]).push(i);
} else {
anagrams.set(wordList[i], [i]);
}
}
// Final result
const result = [];
// Loop through the map
for (let [, value] of anagrams) {
// List of words which are anagrams
const currentWordList = [];
for (let index of value) {
currentWordList.push(words[index]);
}
result.push(currentWordList);
}
return result;
};
const main = () => {
let words = ["CARS", "REPAID", "DUES", "NOSE", "SIGNED", "LANE",
"PAIRED", "ARCS", "GRAB", "USED", "ONES", "BRAG",
"SUED", "LEAN", "SCAR", "DESIGN"];
console.table(groupAnagrams(words));
words = ["CAT", "ACT", "DOG", "TAC", "GOD"];
console.table(groupAnagrams(words));
};
main();