-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgroup-1.js
More file actions
46 lines (39 loc) · 1018 Bytes
/
group-1.js
File metadata and controls
46 lines (39 loc) · 1018 Bytes
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
// prettier-ignore
const alphabet = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
];
/**
* @typedef {string[][]} Trie
*/
/**
* Creates a new dictionary from the text
* @param {string} text
* @returns {Trie}
*/
function parse(text) {
const words = text.split('\n').filter((t) => t.length > 0);
const dictionary = new Array(26);
words.forEach((word) => {
insert(dictionary, word);
});
return dictionary;
}
/**
* Adds a new word to the dictionary
* @param {Trie} dictionary
* @param {string} word
*/
function insert(dictionary, word) {
// Get the index of the first character in the alphabet.
// `index` will be a number from 0 to 25.
const index = alphabet.indexOf(word[0]);
// If a group has not been made for this letter, create it
if (!dictionary[index]) {
dictionary[index] = [];
}
// Push the word to its bucket
dictionary[index].push(word);
}
module.exports = { parse };