This website contains ALL LeetCode **Premium** problems for
**FREE!!**.

All leaked interview problems are collected from Internet.

All leaked interview problems are collected from Internet.

Given an array of strings, group anagrams together.

For example, given: `["eat", "tea", "tan", "ate", "nat", "bat"]`

,

Return:

[ ["ate", "eat","tea"], ["nat","tan"], ["bat"] ]

**Note:** All inputs will be in lower-case.

b'

\n#### Approach #1: Categorize by Sorted String [Accepted]

\n

\n#### Approach #2: Categorize by Count [Accepted]

\n

\n

'
\n\n

\n**Intuition**

Two strings are anagrams if and only if their sorted strings are equal.

\n**Algorithm**

Maintain a map `ans : {String -> List}`

where each key is a sorted string, and each value is the list of strings from the initial input that when sorted, are equal to .

In Java, we will store the key as a string, eg. `code`

. In Python, we will store the key as a hashable tuple, eg. `(\'c\', \'o\', \'d\', \'e\')`

.

**Complexity Analysis**

- \n
- \n
Time Complexity: , where is the length of

\n`strs`

, and is the maximum length of a string in`strs`

. The outer loop has complexity as we iterate through each string. Then, we sort each string in time. \n - \n
Space Complexity: , the total information content stored in

\n`ans`

. \n

\n

**Intuition**

Two strings are anagrams if and only if their character counts (respective number of occurrences of each character) are the same.

\n**Algorithm**

We can transform each string into a character count, , consisting of 26 non-negative integers representing the number of \'s, \'s, \'s, etc. We use these counts as the basis for our hash map.

\nIn Java, the hashable representation of our count will be a string delimited with \'**#**\' characters. For example, `abbccc`

will be `#1#2#3#0#0#0...#0`

where there are 26 entries total. In python, the representation will be a tuple of the counts. For example, `abbccc`

will be `(1, 2, 3, 0, 0, ..., 0)`

, where again there are 26 entries total.

**Complexity Analysis**

- \n
- \n
Time Complexity: , where is the length of

\n`strs`

, and is the maximum length of a string in`strs`

. Counting each string is linear in the size of the string, and we count every string. \n - \n
Space Complexity: , the total information content stored in

\n`ans`

. \n

\n

Analysis written by: @awice

\n