Search⌘ K
AI Features

Solution: Longest Common Suffix Queries

Explore how to use a reversed trie data structure to efficiently find longest common suffix matches between query strings and a given container of words. Understand the process of inserting words in reverse to convert suffix matching into prefix matching, and learn how to handle tie-breaking rules for shortest length and earliest appearance. This lesson helps you implement and analyze a trie-based solution with clear time and space complexity insights.

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 104\leq 10^4

  • 11 \leq wordsContainer[i].length 5103\leq 5 * 10^3 ...