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

Leetcode Problem 745. Prefix and Suffix Search

745. Prefix and Suffix Search

Leetcode Solutions

Trie of Suffix Wrapped Words

  1. Initialize the trie.
  2. For each word in the dictionary, generate all possible suffix-wrapped words including the word itself followed by # and the word again.
  3. Insert each suffix-wrapped word into the trie, associating it with the original word's index.
  4. For a query with a given prefix and suffix, concatenate the suffix, #, and the prefix to form the search string.
  5. Search the trie for the search string and return the associated index of the deepest matching node.
  6. If no match is found, return -1.
UML Thumbnail

Paired Trie

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...