saurus2
Saurus2
saurus2
전체 방문자
오늘
어제
  • 분류 전체보기
    • 개발
      • AJAX
    • ML Ops
    • Profile
    • 음식점
    • 배낭여행
    • 컴퓨터공학
      • 알고리즘 공부
      • C++
      • Sever 스터디
      • Java spring
      • 알고리즘 _ 문제해결
      • 딥러닝
      • Java 정리
      • Python
      • LeetCode 1000
      • Machine Learning Study
      • Sign language Detection Pro..
      • LeetCode Solutions
    • 비콘
    • 데일리 리포트
    • 유학일기
      • 영어 공부
      • Daily
    • AI Master Degree
      • Data Mining
      • AI and Data engineering
      • Math Foundations for Decisi..
      • Natural Language Processing

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • 온라인저지
  • 백준
  • LeetCode
  • 파이썬
  • 문제해결능력
  • DFS
  • 취준
  • two pointer
  • Python
  • 알고리즘
  • 취업준비
  • BFS
  • 딥러닝
  • 릿코드
  • 개발자 취업준비
  • 딕셔너리
  • 리트코드
  • 개발자
  • c++
  • 알고리즘문제해결

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
saurus2

Saurus2

컴퓨터공학/LeetCode 1000

[LeetCode] 49. Group Anagrams

2022. 8. 17. 13:58

49. Group Anagrams

Medium

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.

문제 풀이

문제 접근

  1. 단어의 길이가 최대 100이고 단어 리스트의 최개 길이가 10^4 이다. 
  2. 단어의 길이가 크지 않기 때문에, N^2 시간복잡도 내에 풀어되 될 듯하다.

풀이

  1. 아나그램인지 확인하려면 단어의 알파벳이 일치하는지 확인해야한다.
  2. 정렬을 사용하면 모든 문자의 알파벳들을 비교해볼 수 있다. 
  3. 매 단어를 정렬하여 딕셔너리 리스트에 넣는다. 
  4. 답을 반환할때는 리스트로 반환한다. 

소스 코드

class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        hashmap = defaultdict(list)
        for word in strs:
            temp = word
            t = ''.join(sorted(word))
            hashmap[t].append(temp)
        ans = list(hashmap.values())
        return ans
저작자표시 (새창열림)

'컴퓨터공학 > LeetCode 1000' 카테고리의 다른 글

[LeetCode] 1570. Dot Product of Two Sparse Vectors  (0) 2022.08.18
[LeetCode] 28. Implement strStr()  (0) 2022.08.17
[LeetCode] 804. Unique Morse Code Words  (0) 2022.08.17
[LeetCode] 5. Longest Palindromic Substring  (0) 2022.08.16
[LeetCode] 387. First Unique Character in a String  (0) 2022.08.16
    '컴퓨터공학/LeetCode 1000' 카테고리의 다른 글
    • [LeetCode] 1570. Dot Product of Two Sparse Vectors
    • [LeetCode] 28. Implement strStr()
    • [LeetCode] 804. Unique Morse Code Words
    • [LeetCode] 5. Longest Palindromic Substring
    saurus2
    saurus2
    Simple is Best

    티스토리툴바