diff --git a/Top150Interview/49_Group_Anagrams.py b/Top150Interview/49_Group_Anagrams.py index 15d5e4d..813d369 100644 --- a/Top150Interview/49_Group_Anagrams.py +++ b/Top150Interview/49_Group_Anagrams.py @@ -23,19 +23,27 @@ def groupAnagrams(strs: list[str])->list[str]: :type strs: List[str] :rtype: List[List[str]] """ - grouped = [] + final = [] + seen = [] # Fill first list with matching anagrams for i in range(len(strs)): + grouped = [] + print(f'i is: {strs[i]}') + if i == len(strs)-1 and strs[i] not in grouped: + grouped.append(strs[i]) for j in range(i+1, len(strs)): + print(f'j is: {strs[j]}') if validAnagram(strs[i], strs[j]) == True: - if i in grouped: - if j in grouped: - grouped.append(strs[j]) - else: + if strs[i] not in grouped and strs[i] not in seen: grouped.append(strs[i]) - + seen.append(strs[i]) + if strs[j] not in grouped and strs[j] not in seen: + grouped.append(strs[j]) + seen.append(strs[j]) + if grouped: + final.append(grouped) - return grouped + return final