Applied AI Engineer (LLM) coding learning path Level 3 This level · 14 of 27

Implement Trie (Prefix Tree)

Medium Trie Hash map

Problem

Implement a trie with insert, search and startsWith for lowercase words. search is true only for a complete word, while startsWith is true for any stored word that has that prefix.

Original: LeetCode 208: Implement Trie (Prefix Tree).

Examples

Example 1

Input`Trie: insert("apple"), search("apple"), search("app"), startsWith("app"), insert("app"), search("app")`
Output`[null, true, false, true, null, true]`

Constraints

  • 1 <= word.length, prefix.length <= 2000
  • word and prefix consist only of lowercase English letters.
  • At most 3 * 10^4 calls in total will be made to insert, search, and startsWith.

Hints

Solution

Tried it yourself? The approach, complexity, and code stay hidden until you choose.

Sources (1)
  1. LeetCode 208: Implement Trie (Prefix Tree)leetcode.com
Previous
Next