FastPrepGroup Anagrams

Group Anagrams

EPAM logoEPAM● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given an array of lowercase strings words, group together strings that are anagrams of one another.

For deterministic judging, sort the strings inside each group lexicographically, then sort the groups lexicographically by their full contents.

Function

groupAnagrams(words: String[]) → String[][]

Examples

Example 1

words = ["eat","tea","tan","ate","nat","bat"]return = [["ate","eat","tea"],["bat"],["nat","tan"]]

Each group contains exactly the words with the same character multiset.

Example 2

words = [""]return = [[""]]

The empty string forms one group.

Constraints

  • 1 <= words.length <= 10000.
  • 0 <= words[i].length <= 100.
  • Every word contains lowercase English letters.

Source note: Author-written prompt excerpt retained for source fidelity.

More EPAM problems

See EPAM hiring insights
public String[][] groupAnagrams(String[] words) {
    // Write your code here.
}
words["eat","tea","tan","ate","nat","bat"]
expected[["ate", "eat", "tea"], ["bat"], ["nat", "tan"]]
Checking account…