FastPrepGroup Anagrams

Group Anagrams

Motive logoMotive● MediumFULLTIMEPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Group the strings in strs so that two strings appear in the same group exactly when they are anagrams.

For deterministic output, sort the strings inside each group and then sort the groups lexicographically by their first string.

Function

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

Examples

Example 1

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

Example 2

strs = [""]return = [[""]]

More Motive problems

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