엔지니어 게시판
LeetCode 솔루션 분류

[10/28] 49. Group Anagrams

컨텐츠 정보

본문

Medium
12974387Add to ListShare

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, typically using all the original letters exactly once.

 

Example 1:

Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]

Example 2:

Input: strs = [""]
Output: [[""]]

Example 3:

Input: strs = ["a"]
Output: [["a"]]

 

Constraints:

  • 1 <= strs.length <= 104
  • 0 <= strs[i].length <= 100
  • strs[i] consists of lowercase English letters.
Accepted
1,704,675
Submissions
2,568,825

관련자료

댓글 1

학부유학생님의 댓글

  • 익명
  • 작성일
Runtime: 244 ms, faster than 35.26% of Python3 online submissions for Group Anagrams.
Memory Usage: 18.1 MB, less than 53.63% of Python3 online submissions for Group Anagrams.
from collections import Counter, defaultdict
class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        
        ga = defaultdict(list)
        
        for word in strs:
            ga[tuple(sorted(list(word)))].append(word)
        
        return ga.values()
            
        
전체 404 / 1 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


알림 0