Given an array of strings strs, group the anagrams together. You can return the answer in any order.
An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once.
1 <= strs.length <= 10^40 <= strs[i].length <= 100strs[i] consists of lowercase English letters.No developer has claimed the Python throne for LeetCode #49 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Two strings, bat and atb, what is the fastest way to know that they are anagrams?
Sort each string in alphabetical order, and then compare the sorted strings. If they are equal, then they are anagrams.Click to view the answer
But after sorting, the original string is not taken into account, and the result is the grouping of the original string. How to solve it?
Use tuples, that is, put the alphabetically sorted string and the original string in a tuple, like this: Click to view the answer
("abt", "bat").
All that remains to do is to group this tuple array. What is the easiest way to group?
Use Click to view the answer
Map, key is the alphabetically sorted string, and value is the array of the original string.
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
pairs = [(''.join(sorted(string)), string) for string in strs]
ordered_to_original = defaultdict(list)
for ordered, original in pairs:
ordered_to_original[ordered].append(original)
return list(ordered_to_original.values())
# More concise solution:
class Solution:
def groupAnagrams(self, strs: list[str]) -> list[list[str]]:
s_to_list = defaultdict(list)
for s in strs:
s_to_list[
''.join(sorted(s))
].append(s)
print(s_to_list) # defaultdict(<class 'list'>, {'aet': ['eat', 'tea', 'ate'], 'ant': ['tan', 'nat'], 'abt': ['bat']})
return list(s_to_list.values())
No developer has claimed the Ruby throne for LeetCode #49 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Two strings, bat and atb, what is the fastest way to know that they are anagrams?
Sort each string in alphabetical order, and then compare the sorted strings. If they are equal, then they are anagrams.Click to view the answer
But after sorting, the original string is not taken into account, and the result is the grouping of the original string. How to solve it?
Use tuples, that is, put the alphabetically sorted string and the original string in a tuple, like this: Click to view the answer
("abt", "bat").
All that remains to do is to group this tuple array. What is the easiest way to group?
Use Click to view the answer
Map, key is the alphabetically sorted string, and value is the array of the original string.
# @param {String[]} strs
# @return {String[][]}
def group_anagrams(strs)
result = Hash.new([])
strs.each do |str|
result[str.chars.sort.join] += [str]
end
result.values
end
# Or solution 2: More concise way
# @param {String[]} strs
# @return {String[][]}
def group_anagrams(strs)
strs.group_by { |string| string.chars.sort }.values
end
No developer has claimed the Java throne for LeetCode #49 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the C++ throne for LeetCode #49 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Javascript throne for LeetCode #49 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Go throne for LeetCode #49 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Rust throne for LeetCode #49 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the C# throne for LeetCode #49 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Swift throne for LeetCode #49 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Kotlin throne for LeetCode #49 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.