49.Group-Anagrams

49. Group Anagrams

题目地址

https://www.lintcode.com/problem/anagrams

https://leetcode.com/problems/group-anagrams/

题目描述

Given an array of strings, group anagrams together.

Example:

Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
  ["ate","eat","tea"],
  ["nat","tan"],
  ["bat"]
]

Note:
All inputs will be in lowercase.
The order of your output does not matter.

代码

Approach 1: Categorize by Sorted String

Map => key: sort

Complexity Analysis

  • Time Complexity: O(_N_K_log_K), where N is the length of strs, and K is the maximum length of a string in strs. The outer loop has complexity O(_N) as we iterate through each string. Then, we sort each string in O(_K_log_K) time.

  • Space Complexity: O(_N_K), the total information content stored in ans.

Approach #2 Categorie by Count

Approach #3

Last updated

Was this helpful?