Group Anagrams
Updated
Problem statement
Given an array of strings strs, group together the strings that are anagrams of one another.
Two strings are anagrams when they contain the same letters, each the same number of times. Sort the strings inside each group in lexicographic order. Then sort the groups in lexicographic order of their first string.
Function
groupAnagrams(strs: String[]) → List<List<String>>Examples
Example 1
strs = ["eat","tea","tan","ate","nat","bat"]return = [["ate","eat","tea"],["bat"],["nat","tan"]]ate, eat and tea use the same letters, and so do nat and tan. bat is alone. Each group is sorted inside, and the groups go in the order of their first strings: ate, bat, nat.
Example 2
strs = [""]return = [[""]]The empty string has no letters, and it forms a group of its own.
Example 3
strs = ["ab","ba","abc","cab","bca","x"]return = [["ab","ba"],["abc","bca","cab"],["x"]]The strings ab and ba use the same letters, and so do abc, cab and bca. x is alone. Each group is sorted inside, and the groups go in the order of their first strings.
Constraints
1 <= strs.length <= 10000.0 <= strs[i].length <= 100.- The total number of characters across all strings is at most
100000. - Every string contains only lowercase English letters.
Similar questions at other companies
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Group AnagramsWorkday · PHONE SCREEN
- Bank Account Balances (Part 2): Monthly ReconciliationStripe · PHONE SCREENStripe interview questions
- Time-Aware Search AutocompleteRoblox · PHONE SCREENRoblox interview questions
- Top Ten Most Frequent Words in a BookRobinhood · PHONE SCREENRobinhood interview questions
- Group AnagramsEPAM · PHONE SCREENEPAM interview questions
- Aggregate Matching IPv4 Addresses Across ResponsesPinterest · PHONE SCREENPinterest interview questions
- Diff Two Filesystem Hash TreesCursor · PHONE SCREENCursor interview questions
- Find Duplicate File GroupsAnthropic · PHONE SCREENAnthropic interview questions
- Top K Frequent WordsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions