상위 150개 인터뷰중간

Trie 접두사 트리 구현

'Implement Trie Prefix Tree' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

중간

트리('try'로 발음) 또는 접두사 트리는 문자열 데이터세트에서 키를 효율적으로 저장하고 검색하는 데 사용되는 트리 데이터 구조입니다. 자동 완성 및 맞춤법 검사기와 같은 이 데이터 구조의 다양한 응용 프로그램이 있습니다.

Trie 클래스를 구현합니다.

- Trie() trie 객체를 초기화합니다.

- insert(word: str) 문자열 word를 트라이에 삽입합니다.

- search(word: str) -> bool 문자열 단어가 트리에 있으면(즉, 이전에 삽입된 경우) True를 반환하고 그렇지 않으면 False를 반환합니다.

- startWith(prefix: str) -> bool 접두사 접두사가 있는 이전에 삽입된 문자열 단어가 있으면 True를 반환하고, 그렇지 않으면 False를 반환합니다.

입력은 작업 및 인수 목록입니다. 결과 목록을 반환하는 trie(operations: list, arguments: list) -> list 함수를 구현합니다(생성자/삽입의 경우 None, 검색/startsWith의 경우 bool).

제약
  • 1 <= len(word), len(prefix) <= 2000
  • word and prefix consist of lowercase English letters
  • At most 3 * 10^4 calls will be made in total to insert, search, and startsWith

Example 1
Input
operations = ["Trie", "insert", "search", "search", "startsWith", "insert", "search"], arguments = [[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
Output
[None, None, True, False, True, None, True]
Explanation

Trie initialized. insert("apple"). search("apple") returns True. search("app") returns False. startsWith("app") returns True. insert("app"). search("app") returns True.

Need a Hint?
세트나 힙과 같은 Trie 특정 데이터 구조를 사용하는 것을 고려해보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.