Search⌘ K
AI Features

Solution: Implement Trie (Prefix Tree)

Explore how to implement a Trie data structure in C# to efficiently insert words, search for complete words, and find prefixes. This lesson helps you understand the core logic behind Trie operations, improving your ability to handle string-based coding problems with optimal time and space complexity.

Statement

Trie is a tree-like data structure used to store strings. The tries are also called prefix trees because they provide very efficient prefix-matching operations. Implement a trie data structure with three functions that perform the following tasks:

  • Insert (word): This inserts a word into the trie.
  • Search (word): This searches the given word in the trie and returns TRUE, if found. Otherwise, return FALSE.
  • Search prefix (prefix): This searches the given prefix in the trie and returns TRUE, if found. Otherwise, return FALSE.

Constraints:

  • 11 \leq word.length, prefix.length 500\leq 500
  • The strings consist only of lowercase English letters.
  • At most, 10310^3
...