💡
Exercise 149

Implement Trie 25 XP Medium

LeetCode Ctrl+Enter Run Ctrl+S Save

Implement a Trie with insert, search, and startsWith methods.

Search returns True only if the word exists AND was inserted (is_end=True). StartsWith returns True if any word starts with the prefix.

💡 Pro tip: Understand this problem deeply — don't just memorize the code. Try explaining the approach out loud as if teaching a friend. If you can explain it simply, you truly understand it!

📋 Instructions
Implement Trie and test: insert 'apple', search 'apple' (True), search 'app' (False), startsWith 'app' (True), insert 'app', search 'app' (True).
search: walk to end, return is_end. startsWith: walk to end, return True if all chars found.
🧪 Test Cases
Input
t.search('apple')
Expected
True
Boolean check
Input
t.search('app')
Expected
False
Boolean check
Input
t.startsWith('app')
Expected
True
Boolean check
Input
t.search('app')
Expected
True
Boolean check
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.