Description
https://leetcode.com/problems/group-anagrams/description/
Naive Solution —— implement your own hash
1 | class Solution: |
But the performance is so pool.
Sorting
Sort the word by char, then use the sorted string as key in hash table
Trade Off
Given the length of word is n
The time consuming on each item is O(n x logn) on sorting method, the one on the other method is (5*n)
If the length of the word is short, the constant item is quite large compared to the length of the word, which is the case in this question. so the your-own-hash way performance poorly.