Group Experiment Buckets
An experimentation platform labels each user's variant exposure as a short string of feature letters. Two exposures are 'equivalent' when they contain the same letters in any order. Group equivalent exposure strings together for cohort analysis.
Problem
Given an array of strings, group the strings that are anagrams of one another. Return the groups (the order of groups and of strings within a group does not matter).
Input
An array words of length n (1 ≤ n ≤ 10^4); each word has length up to 100 lowercase letters.
Output
A list of groups, each group a list of strings that are mutual anagrams.
Constraints
- 1 ≤ n ≤ 10,000
- Each word length ≤ 100, lowercase a-z only
- Group/word order is unconstrained
Examples
Example 1
Input
words = ["eat","tea","tan","ate","nat","bat"]
Output
[["eat","tea","ate"],["tan","nat"],["bat"]]
eat/tea/ate share letters {a,e,t}; tan/nat share {a,n,t}; bat is alone.
Example 2
Input
words = ["abc","cba","xyz"]
Output
[["abc","cba"],["xyz"]]
abc and cba are anagrams; xyz is its own group.