50 lines
1.7 KiB
Python
50 lines
1.7 KiB
Python
def validAnagram(s: str, t: str) -> bool:
|
|
seen = {}
|
|
check = {}
|
|
count = 0
|
|
for letter in s:
|
|
if letter in seen:
|
|
seen[letter] += 1
|
|
else:
|
|
seen[letter] = 1
|
|
for letter in t:
|
|
if letter in check:
|
|
check[letter] += 1
|
|
else:
|
|
check[letter] = 1
|
|
if seen != check:
|
|
return False
|
|
else:
|
|
return True
|
|
|
|
|
|
def groupAnagrams(strs: list[str])->list[str]:
|
|
"""
|
|
:type strs: List[str]
|
|
:rtype: List[List[str]]
|
|
"""
|
|
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 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 final
|
|
|
|
|
|
|
|
print(groupAnagrams(["eat","tea","tan","ate","nat","bat"])) |