Problem Description
Given two arrays of strings, wordsContainer and wordsQuery, for each query string in wordsQuery find the index of a string in wordsContainer that shares the longest common suffix with it. If multiple words share the same longest common suffix then choose the one with the smallest length, and if there is still a tie, choose the one that appears earliest in wordsContainer.
Key Insights
- Reverse the strings so that finding a common suffix becomes equivalent to finding a common prefix.
- Build a Trie (prefix tree) using the reversed words from wordsContainer.
- At each Trie node, store the best candidate word (based on smallest overall length, then smallest index) among all words that pass through that node.
- For each query, traverse the Trie according to the reversed query string to determine how many characters match and use the stored candidate from the last matched node.
- If no characters match (i.e. common suffix is empty), use the candidate from the root, which represents the empty suffix shared by all.
Space and Time Complexity
Time Complexity: O(S + Q) where S is the sum of lengths of all strings in wordsContainer and Q is the sum of lengths of all strings in wordsQuery. Space Complexity: O(S) for the Trie data structure.
Solution
We solve the problem by building a Trie that is constructed from the reversed words in wordsContainer. Each node in the Trie holds a candidate that is the best index yielding a word with the smallest length (and if lengths tie, then the smallest index) among all words sharing that prefix. While inserting each word (in reversed form) into the Trie, the candidate stored at each node is updated if the current word is a better candidate according to our criteria.
For each query, we reverse it, then traverse the Trie along its characters. We continue as long as children exist matching the characters of the reversed query string. The candidate stored at the deepest node reached gives us the answer since that node corresponds to the longest common suffix. If at any point a character does not exist in the Trie, we stop and use the candidate from the last valid node (or the root if no characters match). This ensures we are returning the index of the word from wordsContainer that has the longest common suffix with the query, with tie-breaking handled by the candidate stored at each node.