You are given two arrays of strings wordsContainer and wordsQuery.
For each wordsQuery[i], you need to find a string from wordsContainer that has the longest common suffix with wordsQuery[i]. If there are two or more strings in wordsContainer that share the longest common suffix, find the string that is the smallest in length. If there are two or more such strings that have the same smallest length, find the one that occurred
earlier in wordsContainer.
Return an array of integersans, whereans[i]is the index of the string inwordsContainerthat has thelongest common suffix with wordsQuery[i].
Input: wordsContainer =["abcd","bcd","xbcd"], wordsQuery =["cd","bcd","xyz"]Output: [1,1,1]Explanation:
Let's look at each `wordsQuery[i]` separately:* For `wordsQuery[0] = "cd"`, strings from `wordsContainer` that share the longest common suffix `"cd"` are at indices 0,1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.* For `wordsQuery[1] = "bcd"`, strings from `wordsContainer` that share the longest common suffix `"bcd"` are at indices 0,1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.* For `wordsQuery[2] = "xyz"`, there is no string from `wordsContainer` that shares a common suffix. Hence the longest common suffix is`""`, that issharedwith strings at index 0,1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.
Input: wordsContainer =["abcdefgh","poiuygh","ghghgh"], wordsQuery =["gh","acbfgh","acbfegh"]Output: [2,0,2]Explanation:
Let's look at each `wordsQuery[i]` separately:* For `wordsQuery[0] = "gh"`, strings from `wordsContainer` that share the longest common suffix `"gh"` are at indices 0,1, and 2. Among these, the answer is the string at index 2 because it has the shortest length of 6.* For `wordsQuery[1] = "acbfgh"`, only the string at index 0 shares the longest common suffix `"fgh"`. Hence it is the answer, even though the string at index 2is shorter.* For `wordsQuery[2] = "acbfegh"`, strings from `wordsContainer` that share the longest common suffix `"gh"` are at indices 0,1, and 2. Among these, the answer is the string at index 2 because it has the shortest length of 6.
To efficiently find the longest common suffix, we can build a trie using the reversed strings from wordsContainer. For each query, we reverse it and traverse the trie to find the deepest match, keeping track of the best candidate index, length, and tie-breaking by length and order.
Reverse all strings in wordsContainer and build a trie, storing at each node the best candidate index and length so far.
For each query, reverse it and traverse the trie as far as possible, tracking the best candidate index (longest match, shortest length, earliest index).
classTrieNode:
def__init__(self):
self.children = {}
self.idx =-1 self.len =0classSolution:
defstringIndices(self, wordsContainer: list[str], wordsQuery: list[str]) -> list[int]:
root = TrieNode()
for i, w in enumerate(wordsContainer):
s = w[::-1]
node = root
for c in s:
if c notin node.children:
node.children[c] = TrieNode()
node = node.children[c]
if node.len < len(w) or (node.len == len(w) and node.idx > i):
node.len = len(w)
node.idx = i
ans = []
for q in wordsQuery:
s = q[::-1]
node = root
best =-1 bestLen =-1for i, c in enumerate(s):
if c notin node.children:
break node = node.children[c]
if node.idx !=-1and (i+1> bestLen or (i+1== bestLen and (len(wordsContainer[node.idx]) < len(wordsContainer[best]) if best !=-1elseTrue) or (i+1== bestLen and len(wordsContainer[node.idx]) == len(wordsContainer[best]) and node.idx < best))):
best = node.idx
bestLen = i+1if best ==-1:
best =0 ans.append(best)
return ans
1
// Omitted for brevity. See Python for main logic.
1
// Omitted for brevity. See Python for main logic.
⏰ Time complexity: O(NL + QL), where N is the number of words in wordsContainer, Q is the number of queries, and L is the average word length. Each word and query is processed once.