Easy Python solution


  • 0
    class Solution(object):
        def groupAnagrams(self, strs):
            n = len(newStrs)
            if n == 0: return []
            newStrs = []
            for str in strs:
                newStrs.append(''.join(sorted(str)))
            strs = [x for (y,x) in sorted(zip(newStrs,strs))]
            newStrs.sort()
            re = [[strs[0]]]
            for i in xrange(1, n):
                if newStrs[i] != newStrs[i-1]: re.append([strs[i]])
                else: re[-1].append(strs[i])
            return re
    

    Not an efficient way but very easy to come up and write out during the interview.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.