From 909465a1ee902e1397d6edc8a72bf3a5805dda89 Mon Sep 17 00:00:00 2001 From: ImAlpha Date: Sun, 6 Aug 2023 15:48:17 -0700 Subject: [PATCH] valid for 40/70 test cases --- Top150Interview/49_Group_Anagrams.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) 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