FastPrepGroup Anagram Names

Group Anagram Names

WhatNot logoWhatNot● MediumINTERNPHONE SCREEN
Learn

Problem statement

Given an array of lowercase names names, group names that are anagrams of one another. Two names are anagrams when every letter appears the same number of times in both names.

  • Every input occurrence must appear exactly once in the result. Keep duplicate names.
  • Order groups by the first appearance of any member of that group in names.
  • Within each group, keep names in their original input order.
  • Return an empty list when names is empty.

Function

groupAnagramNames(names: String[]) → List<List<String>>

Examples

Example 1

names = ["amy","may","bob","yma","obb"]return = [["amy","may","yma"],["bob","obb"]]

The first group is created by amy. Its later anagrams stay in input order; bob starts the second group.

Example 2

names = ["anna","anan","ann","anna"]return = [["anna","anan","anna"],["ann"]]

Repeated names are retained. The three-letter name ann has different character counts and belongs to another group.

Example 3

names = []return = []

An empty input produces no groups.

Constraints

  • 0 <= names.length <= 1000.
  • Each name contains only lowercase English letters and has length from 1 to 50.
  • The total number of characters is at most 5000.

More WhatNot problems

See WhatNot hiring insights
public List<List<String>> groupAnagramNames(String[] names) {
    // write your code here
}
names["amy","may","bob","yma","obb"]
expected[["amy","may","yma"],["bob","obb"]]
Checking account…