Problem overview
The method groupByFirstLetter(words) receives an array of lowercase words. It returns an object where each key is a first letter and each value is an array of all the words that start with that letter, in the order they appear in words.
For ["dog", "cat", "deer", "cow", "duck"] the result is {"d": ["dog", "deer", "duck"], "c": ["cat", "cow"]}. "dog" comes before "deer" and "duck" in the input, so it also comes first in the "d" group.
Approach
This is a dictionary of lists. Each word belongs to exactly one group, chosen by its first letter, so a single pass is enough. For each word, find its group; if the group doesn't exist yet, create it as an empty array; then add the word to it.
The creation step is separate from the adding step. Creating the group only when it's missing means the first word of a letter starts the group, and later words of that letter find it already there. The add step then runs for every word, whether the group is new or not.
Step by step
- Create an empty object named
groupswithconst. - Loop over the words with
for (const word of words). - Take the first letter with
const first = word[0]. It is the key of this word's group. - Create the group if it is missing. The check is
!(first in groups), and the body setsgroups[first] = []. - Push the word onto
groups[first]. - Return
groupsafter the loop.
The solution in JavaScript
class Solution {
groupByFirstLetter(words) {
const groups = {};
for (const word of words) {
const first = word[0];
if (!(first in groups)) {
groups[first] = [];
}
groups[first].push(word);
}
return groups;
}
}
The details to notice:
- Indexing a string.
word[0]is a one-character string, such as"d". That is the key type the lesson asks for. - The parentheses in
!(first in groups). Without them,!first in groupswould apply!tofirstbefore theincheck, and then check whether a key named"false"exists. The parentheses make!negate the whole check. - Arrays are shared by reference.
groups[first]is the array stored in the object, not a copy. Callingpushon it changes the array insidegroups, so no assignment back is needed. constinside the loop.wordandfirstare fresh on every pass, soconstis fine.
Tracing the example
For ["dog", "cat", "deer", "cow", "duck"]: "dog" has no "d" group yet, so one is created and "dog" is added. "cat" creates "c". "deer" finds the "d" group and joins it, then "cow" joins "c", and "duck" joins "d". The final object is {d: ["dog", "deer", "duck"], c: ["cat", "cow"]}. The groups keep the words in input order because the loop meets them in that order and push adds at the end.
Edge cases and common mistakes
- Replacing the group every time. If the line
groups[first] = []is not inside theif, every word wipes its group, and each ends up with only its last word. - Pushing before the group exists.
groups[first]would beundefined, and callingpushon it throws aTypeError. - Repeated words. They are kept.
["go", "gone", "go"]gives{g: ["go", "gone", "go"]}. - One word.
["hello"]gives{h: ["hello"]}. - Empty strings. The lesson guarantees at least one letter. For an empty word,
word[0]isundefined, which would become a key named"undefined".
Testing
["apple", "banana", "avocado", "blueberry", "cherry"]gives{a: ["apple", "avocado"], b: ["banana", "blueberry"], c: ["cherry"]}.["dog", "cat", "deer", "cow", "duck"]gives{d: ["dog", "deer", "duck"], c: ["cat", "cow"]}.["hello"]gives{h: ["hello"]}.["go", "gone", "go"]gives{g: ["go", "gone", "go"]}.
