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 integers ans, where ans[i] is the index of the string in wordsContainer that has the longest common suffix with wordsQuery[i].
Example 1:
Input: wordsContainer = ["abcd","bcd","xbcd"], wordsQuery = ["cd","bcd","xyz"]
Output: [1,1,1]
Explanation:
Let's look at each wordsQuery[i] separately:
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.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.wordsQuery[2] = "xyz", there is no string from wordsContainer that shares a common suffix. Hence the longest common suffix is "", that is shared with 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.Example 2:
Input: wordsContainer = ["abcdefgh","poiuygh","ghghgh"], wordsQuery = ["gh","acbfgh","acbfegh"]
Output: [2,0,2]
Explanation:
Let's look at each wordsQuery[i] separately:
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.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 2 is shorter.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.Constraints:
1 <= wordsContainer.length, wordsQuery.length <= 1041 <= wordsContainer[i].length <= 5 * 1031 <= wordsQuery[i].length <= 5 * 103wordsContainer[i] consists only of lowercase English letters.wordsQuery[i] consists only of lowercase English letters.wordsContainer[i].length is at most 5 * 105.wordsQuery[i].length is at most 5 * 105.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the longest common suffix between multiple words involves checking all possible suffix lengths. We start from the shortest possible length and gradually increase it, checking for a match across all words at each step. We stop once we've exhausted all possibilities.
Here's how the algorithm would work step-by-step:
def longest_common_suffix_brute_force(words):
shortest_word_length = min(len(word) for word in words)
longest_common_suffix = ""
# The suffix cannot be longer than the shortest word.
for suffix_length in range(1, shortest_word_length + 1):
suffix = words[0][-suffix_length:]
# Compare the current suffix against all words.
is_common = True
for word in words:
if not word.endswith(suffix):
is_common = False
break
if is_common:
# Update the longest common suffix.
longest_common_suffix = suffix
# Return the longest common suffix or an empty string if none exists.
return longest_common_suffixThe key to efficiently solving this problem is to avoid redundant comparisons. We'll construct a special data structure to quickly find the longest shared ending between any two words, dramatically speeding up the query process.
Here's how the algorithm would work step-by-step:
def longest_common_suffix(strings, queries):
results = []
for index_one, index_two in queries:
string_one = strings[index_one]
string_two = strings[index_two]
# Determine minimum length to avoid index errors
minimum_length = min(len(string_one), len(string_two))
common_suffix_length = 0
# Iterate backwards to find the longest common suffix
for i in range(1, minimum_length + 1):
if string_one[-i] == string_two[-i]:
common_suffix_length += 1
else:
break
results.append(common_suffix_length)
return results| Case | How to Handle |
|---|---|
| Empty list of strings | Return an empty list of results indicating no common suffixes. |
| Queries list is empty | Return an empty list since there are no suffix queries to perform. |
| One or more strings in the string list are empty | An empty string has an empty common suffix with other strings; handle gracefully in comparison. |
| One or more strings are null | Treat null strings as empty strings or throw an IllegalArgumentException if nulls are not permitted. |
| Queries with the same string indices | Ensure each query is processed correctly even if it asks for the same pair of strings multiple times. |
| Long strings causing excessive memory usage | Consider limiting the maximum length of strings allowed and allocate memory efficiently. |
| Large number of strings causing excessive memory usage or time complexity | Optimize string comparisons to minimize time complexity and consider using a trie-like structure. |
| String indices in queries are out of bounds | Check query indices against the size of the input string list and return an error or a specific value like -1. |