bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 642. Design Search Autocomplete System

642. Design Search Autocomplete System

Leetcode Solutions

Trie with Priority Queue

  1. Define a TrieNode class with a children map and a sentences map.
  2. Implement the addToTrie method to insert sentences into the trie.
  3. Initialize the AutocompleteSystem with a root TrieNode.
  4. For each input character, traverse the trie and update the current TrieNode.
  5. If the character is '#', add the current sentence to the trie and reset.
  6. If the character is not '#', use a priority queue to find the top 3 sentences.
  7. Return the top 3 sentences or as many as available.
UML Thumbnail

Brute Force Search with Sorting

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...