Group Anagrams
Category: Strings
```
Problem Statement
Group Anagrams Write a function that takes in an array of strings and groups anagrams together. Anagrams are strings made up of exactly the same letters, where order doesn't matter. For example, "cinema" and "iceman" are anagrams; similarly, "foo" and "ofo" are anagrams. Your function should return a list of anagram groups in no particular order. Sample Input words = ["yo", "act", "flop", "tac", "cat", "oy", "olfp"] Sample Output [["yo", "oy"], ["flop", "olfp"], ["act", "tac", "cat"]] Hints Hint 1 Try rearranging every input string such that each string's letters are ordered in alphabetical order. What can you do with the resulting strings? Hint 2 For any two of the resulting strings mentioned in Hint #1 that are equal to each other, their original strings (with their letters in normal order) must be anagrams. Realizing this, you could bucket all of these resulting strings together, all the while keeping track of their original strings, to nd the groups of anagrams. Hint 3 Can you simply store the resulting strings mentioned in Hint #1 in a hash table and nd the groups of anagrams using this hash table? Optimal Space & Time Complexity O(w * n * log(n)) time | O(wn) space - where w is the number of words and n is the length of the longest word
```
Approach & Solution
Solution 1
```
Solution 1 Solution 2
import java.util.;
import java.util.stream.;
class Program {
// O(w * n * log(n)) time | O(wn) space - where w is the number of words and n is the length of
// the longest word
public static List> groupAnagrams(List
> output = new ArrayList
>();
for (Map.Entry
Solution 2
```
Solution 1 Solution 2
2
3 import java.util.;
4 import java.util.stream.;
5
6 class Program {
7 // O(w * n * log(n) + n * w * log(w)) time | O(wn) space - where w is the number of words and
8 // n is the length of the longest word
9 public static List> groupAnagrams(List
>();
11
12 List
> result = new ArrayList
>();
24 List
```
Test Cases
``` Test Case 1 {"words": ["yo", "act", "flop", "tac", "cat", "oy", "olfp"]} Test Case 2 {"words": []} Test Case 3 {"words": ["test"]} Test Case 4 {"words": ["abc", "dabd", "bca", "cab", "ddba"]} Test Case 5 {"words": ["abc", "cba", "bca"]} Test Case 6 {"words": ["zxc", "asd", "weq", "sda", "qwe", "xcz"]} Test Case 7 {"words": ["cinema", "a", "flop", "iceman", "meacyne", "lofp", "olfp"]} Test Case 8 {"words": ["abc", "abe", "abf", "abg"]}