Search⌘ K
AI Features

Solution: Longest Common Suffix Queries

Explore building and querying a reversed trie to solve longest common suffix queries efficiently. Learn to insert words in reverse order, update candidates at each node, and retrieve the best matching indices. This lesson helps you handle large arrays of strings with suffix-based lookups, improving your understanding of trie structures and string matching algorithms.

Statement

You are given two arrays of strings, wordsContainer and wordsQuery.

For each string wordsQuery[i], find the string in wordsContainer that shares the longest common suffix with it.

  • If multiple strings in wordsContainer share the same longest suffix, choose the one with the smallest length.

  • If two or more such strings have the same smallest length, choose the string that appears earliest in wordsContainer.

Return an array of integers ans, where ans[i] is the index of the chosen string in wordsContainer for the query wordsQuery[i].

Constraints:

  • 11 \leq wordsContainer.length, wordsQuery.length ...