group anagram started, used valid anagram for method
This commit is contained in:
parent
866df8030f
commit
fecfc3da14
@ -1,5 +1,42 @@
|
|||||||
def groupAnagrams(self, strs):
|
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]
|
:type strs: List[str]
|
||||||
:rtype: List[List[str]]
|
:rtype: List[List[str]]
|
||||||
"""
|
"""
|
||||||
|
grouped = []
|
||||||
|
# Fill first list with matching anagrams
|
||||||
|
for i in range(len(strs)):
|
||||||
|
for j in range(i+1, len(strs)):
|
||||||
|
if validAnagram(strs[i], strs[j]) == True:
|
||||||
|
if i in grouped:
|
||||||
|
if j in grouped:
|
||||||
|
grouped.append(strs[j])
|
||||||
|
else:
|
||||||
|
grouped.append(strs[i])
|
||||||
|
|
||||||
|
|
||||||
|
return grouped
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
print(groupAnagrams(["eat","tea","tan","ate","nat","bat"]))
|
||||||
Loading…
Reference in New Issue
Block a user