본문 바로가기

Trie3

[Python] 트라이 자료구조 구현(Trie) 트라이 자료구조는 검색에 최적화된 자료구조이다 자식 노드가 여러 개인 트리 형태로 구성된다고 이해하면 된다. https://leetcode.com/problems/implement-trie-prefix-tree/ Implement Trie (Prefix Tree) - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 위의 문제를 기준으로 구현했다. class TrieNode: def __init__(self): self.word = False self.childre.. 2022. 4. 30.
[프로그래머스] [3차] 자동완성(Python) https://programmers.co.kr/learn/courses/30/lessons/17685 코딩테스트 연습 - [3차] 자동완성 자동완성 포털 다음에서 검색어 자동완성 기능을 넣고 싶은 라이언은 한 번 입력된 문자열을 학습해서 다음 입력 때 활용하고 싶어 졌다. 예를 들어, go 가 한 번 입력되었다면, 다음 사용자는 g programmers.co.kr Trie 알고리즘을 활용해서 풀었다. 나의 풀이 class Trie(): def __init__(self): self.child = dict() self.count = 0 def insert(self,string): curr = self for str in string: if str not in curr.child: curr.child[str].. 2022. 2. 7.
[프로그래머스] 가사 검색 (Python) https://programmers.co.kr/learn/courses/30/lessons/60060 코딩테스트 연습 - 가사 검색 programmers.co.kr 문자열 검색 알고리즘인 Trie를 활용해서 푸는 문제다 계속 효율성 4번에서 시간 초과가 나서 풀이를 참고하고 그대로 구현했다. class Trie(): def __init__(self): self.child = dict() self.count = 0 def insert(self,str): curr = self for ch in str: curr.count += 1 if ch not in curr.child: curr.child[ch] = Trie() curr = curr.child[ch] curr.count += 1 def search(se.. 2021. 12. 27.