Quick actions

cmd+k|ctrl+k

Navigation

Languages

leetcode problem 49

Snippet info

Language

Python

Visibility

public

Author

vminotra

Created

2020-08-12T12:59:26Z

Updated

2020-08-15T17:13:42Z

# leetcode problem 49
# https://leetcode.com/problems/group-anagrams/

class Solution:
    
    def __init__(self):
        
        self.dict1 = dict()                
    
    def groupAnagrams(self, strs):
        
        strs_len = len(strs)
        
        for i, word in enumerate(strs):
            
            char_dict = dict()
            for alpha in word:
                if alpha in char_dict:
                    char_dict[alpha] += 1
                else:
                    char_dict[alpha] = 1
                    
            self.dict1[i] = char_dict                
        
        res_list = []
        words_matched = []
        
        for i in range(0, strs_len):
            
            if i in words_matched:
                continue
                
            res = [strs[i]]    
            words_matched.append(i)
            for j in range(i+1, strs_len):
                
                if j in words_matched:
                    continue
                    
                if self.dict1[i] == self.dict1[j]:
                    res.append(strs[j])
                    words_matched.append(j)
            
            res_list.append(res)
            
        return res_list
 
# strs = ["hos","boo","nay","deb","wow","bop","bob","brr","hey","rye","eve","elf","pup","bum","iva","lyx","yap","ugh","hem","rod","aha","nam","gap","yea","doc","pen","job","dis","max","oho","jed","lye","ram","pup","qua","ugh","mir","nap","deb","hog","let","gym","bye","lon","aft","eel","sol","jab"]      
# solution = Solution()
# result = solution.groupAnagrams(strs)
# print(result)

l2 = []
if l2 is []:
    print("aaa")
INFO