문제: https://leetcode.com/problems/group-anagrams/
import Foundation
class Solution {
func groupAnagrams(_ strs: [String]) -> [[String]] {
var dict = [String: [String]]()
for str in strs {
let key = String(str.sorted())
if dict[key] == nil {
dict[key] = [str]
} else {
dict[key]!.append(str)
}
}
return Array(dict.values)
}
}
print(Solution().groupAnagrams(["eat","tea","tan","ate","nat","bat"])) // [["bat"],["nat","tan"],["ate","eat","tea"]]
print(Solution().groupAnagrams([""]))
print(Solution().groupAnagrams(["a"]))
print(Solution().groupAnagrams(["ddddddddddg","dgggggggggg"]))
'알고리즘' 카테고리의 다른 글
[Swift] LeetCode - 20. Valid Parentheses (0) | 2022.02.15 |
---|---|
[Swift] LeetCode - 2. Add Two Numbers (0) | 2022.02.15 |
[Swift] LeetCode - 819. Most Common Word (0) | 2022.02.14 |
[Swift] LeetCode - 937. Reorder Data in Log Files (0) | 2022.02.14 |
[Swift] 프로그래머스 - 후보키 (0) | 2022.02.11 |
댓글