Data Structures19 sections · 729 units
Open in Course

Implement Trie Solution

Build your trie

Implement the trie with all three operations. Design choices:

  • Hash map vs. array for children: Hash map is flexible (any character set). Array is faster for fixed alphabets (constant-time child lookup).
  • Separate class for node: Cleaner code, easier to extend with additional fields.

Testing checklist:

  • Insert and search same word
  • Search for prefix of existing word (should return false)
  • startsWith for prefix of existing word (should return true)
  • Search/startsWith for non-existent strings

Time: O(L)O(L) for all operations. Space: O(total characters across all words)O(\text{total characters across all words}).