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.
1 2 3 4 5 6 7 8 |
func groupAnagrams(_ strs: [String]) -> [[String]] { var dict = [String: [String]]() strs.forEach { let s = String($0.sorted()) dict[s] = (dict[s] == nil) ? [$0] : dict[s]! + [$0] } return dict.map({$0.value}) } |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼