Skip to content

Latest commit

 

History

History
40 lines (25 loc) · 919 Bytes

49. Group Anagrams.md

File metadata and controls

40 lines (25 loc) · 919 Bytes

Leetcode

49. Group Anagrams

문제링크

해시테이블 문제

: anagram(순서만 뒤바뀐 같은 문자로 구성된 문자열) 끼리 묶은 배열을 리턴하는 문제

코드

class Solution {
    
    func groupAnagrams(_ strs: [String]) -> [[String]] {
        
        var hashTable = [[Character] : [String]]()
        
        for str in strs {
            let key = str.sorted()
            if hashTable[key] == nil { hashTable[key] = [str] }
            else { hashTable[key]?.append(str) }
        }
        let result = hashTable.values.map { Array($0) }
        return result
    }
    
}

풀이 설명

  • hash func = str.sorted()
  • hash table = result

라고 볼 수 있다