[LeetCode] 49. Group Anagrams

2023. 8. 10. 21:03·Problem Solving/LeetCode

https://leetcode.com/problems/group-anagrams/description/

 

Group Anagrams - LeetCode

Can you solve this real interview question? Group Anagrams - 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

leetcode.com

 

 

class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        hm = defaultdict(list)
        for word in strs :
            hm["".join(sorted(word))].append(word)
        return list(hm.values())

파이썬에서 사전순으로 문자열 정렬하기

a = ['da', 'ef', 'ba']
a.sort()
print(a) 

# ['ba', 'da', 'ef']

리스트에서 해당 문자열 자체를 정렬하고 싶다면

s = "bqab"

s1 = s.sort() # str type에는 sort()라는 메소드가 없다. (String의 경우 첫글자의
							# 주소값으로 참조를 하기에 원본이 변경되면 안된다.)
s2 = sorted(s) # ['a', 'b', 'b', 'q'] => return type -> list
s3 = ''.joint(sorted(s)) # "abbq"

 

'Problem Solving/LeetCode' 카테고리의 다른 글
  • [LeetCode] 347. Top K Frequent Elements
  • [LeetCode] 746. Min Cost Climbing Stairs
  • [LeetCode] 2811. Check if it is Possible to Split Array
  • [LeetCode] 33. Search in Rotated Sorted Array.
kimdozzi
kimdozzi
끝까지 포기하지 않으면, 내가 다 이겨!
  • kimdozzi
    도브로
    kimdozzi
  • 전체
    오늘
    어제
    • 분류 전체보기 (132)
      • Problem Solving (49)
        • Baekjoon (29)
        • Programmers (0)
        • LeetCode (17)
        • 삼성 유형 (2)
      • Computer Science (27)
        • Operating System (2)
        • Algorithms (13)
        • Network (6)
        • DataBase (6)
      • Backend (33)
        • JavaScript (0)
        • TypeScript (6)
        • Java (7)
        • Spring Boot (7)
        • Spring Security (6)
        • JPA (2)
        • Mybatis (1)
        • Junit5 (1)
        • Redis (3)
      • DevOps (14)
        • Git, Github (5)
        • docker (4)
        • AWS (3)
        • nginx (2)
      • etc (6)
        • IntelliJ (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 티스토리
    • 설정
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    인덱스 시그니처
    AWS
    python
    파이썬
    인덱서블 타입
    티스토리챌린지
    인터페이스
    imos법
    Bucket
    삼성기출
    누적합
    세그먼트 트리
    도커
    docker
    컨테이너
    CORS
    S3
    오블완
    interface
    온라인 쿼리
    오프라인 쿼리
    TypeScript
    백준
    segment tree
    타입스크립트
    PrefixSum
    docker image
    구간합
    알고리즘
    구간 업데이트
    점 업데이트
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
kimdozzi
[LeetCode] 49. Group Anagrams
상단으로

티스토리툴바