Skip to content

Latest commit

 

History

History
28 lines (24 loc) · 514 Bytes

L49 group-anagrams.md

File metadata and controls

28 lines (24 loc) · 514 Bytes

group-anagrams

Given an array of strings, group anagrams together.

Example:

Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","tan"], ["bat"] ]

class Solution:
	def groupAnagrams(self, strs):
		dic={}
		for i in range(len(strs)):
			l="".join(sorted(strs[i]))
			if l in dic.keys():
				dic[l]=dic[l]+[strs[i]]
			else:
				dic[l]=[strs[i]]
		return list(dic.values())

 #如果有x这个key则value赋值为0,若有则在上面加1
dic[x]=dic.get(x,0)+1