Group Anagrams
在当下绝对是简单题了,uber考过要求不用sort string实现,可以统计字符个数encode。比如aabd=a2b1d1,用数组表示hash表。这题python实现有个catch,因为input是strs,很容易用str作为单个string的表示。这样hide了str()函数,报错:TypeError: 'unicode' object is not callable
class Solution(object):
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
def getHash(s):
hd = [0]*256
for c in s:
hd[ord(c)]+=1
res = []
for i in range(256):
if hd[i]!=0:
res.append(chr(i)+str(hd[i]))
return ''.join(res)
hsd = {}
for s in strs:
hs = getHash(s)
if hs not in hsd:
hsd[hs]=list()
hsd[hs].append(s)
res = []
for k in hsd:
hsd[k].sort()
res.append(hsd[k])
return res