FastPrepGroup Anagrams

Group Anagrams

Microsoft logoMicrosoft● MediumNEW GRADONSITE 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 lexicographically, then sort the groups lexicographically by their complete arrays.

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 = [[""]]

Constraints

  • 1 <= strs.length <= 10^4
  • 0 <= strs[i].length <= 100
  • Every string contains only lowercase English letters.
  • The total number of characters is at most 10^5.

More Microsoft problems

See Microsoft 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…