Given an array words of lowercase words, group the words by their first letter. Return a mutable.Map where each key is a first letter, written as a one-character string, and its value is an array of all the words that start with that letter.
Inside each group, keep the words in the order they have in words. The order of the entries in the mutable.Map doesn't matter.
Example 1:
Input: words = ["apple", "banana", "avocado", "blueberry", "cherry"]
Output: {"a": ["apple", "avocado"], "b": ["banana", "blueberry"], "c": ["cherry"]}
Explanation: "apple" and "avocado" start with "a", "banana" and "blueberry" start with "b", and "cherry" is alone under "c".
Example 2:
Input: words = ["dog", "cat", "deer", "cow", "duck"]
Output: {"d": ["dog", "deer", "duck"], "c": ["cat", "cow"]}
Explanation: "dog" comes before "deer" and "duck" in words, so it comes first in the "d" group too.
Example 3:
Input: words = ["hello"]
Output: {"h": ["hello"]}
Explanation: One word makes one group.
Rules:
wordshas at least one word.- Every word has at least one letter and only lowercase letters a-z.
- A word that appears more than once in
wordsappears as many times in its group.
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
