Solving Leetcode Interviews in Seconds with AI: Longest Common Suffix Queries
Introduction
In this blog post, we will explore how to solve the LeetCode problem "3093" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
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: 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 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: 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 2 is 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. Constraints: 1 <= wordsContainer.length, wordsQuery.length <= 104 1 <= wordsContainer[i].length <= 5 103 1 <= wordsQuery[i].length <= 5 103 wordsContainer[i] consists only of lowercase English letters. wordsQuery[i] consists only of lowercase English letters. Sum of wordsContainer[i].length is at most 5 105. Sum of wordsQuery[i].length is at most 5 105.
Explanation
Here's a breakdown of the approach, complexity, and the Python code:
High-Level Approach:
- Iterate through each query word.
- For each query word, iterate through the container words to find the longest common suffix and its length. Keep track of the best match (index, length, container word length) based on the problem's criteria.
- Store the index of the best match for each query in the result array.
Complexity:
- Runtime: O(M N L), where M is the number of query words, N is the number of container words, and L is the maximum length of any word.
- Storage: O(M), where M is the number of query words (for the result array).
Code
def solve():
wordsContainer = ["abcd","bcd","xbcd"]
wordsQuery = ["cd","bcd","xyz"]
print(longest_common_suffix(wordsContainer, wordsQuery))
def longest_common_suffix(wordsContainer, wordsQuery):
ans = []
for query in wordsQuery:
best_index = -1
max_len = -1
min_len = float('inf')
for i, container_word in enumerate(wordsContainer):
common_len = 0
min_word_len = float('inf')
j = len(query) - 1
k = len(container_word) - 1
while j >= 0 and k >= 0 and query[j] == container_word[k]:
common_len += 1
j -= 1
k -= 1
if common_len > max_len:
max_len = common_len
best_index = i
min_len = len(wordsContainer[i])
elif common_len == max_len and common_len > -1:
if len(wordsContainer[i]) < min_len:
min_len = len(wordsContainer[i])
best_index = i
if best_index == -1 and len(wordsContainer) > 0:
min_word_len = float('inf')
best_index = 0
for i, container_word in enumerate(wordsContainer):
if len(container_word) < min_word_len:
min_word_len = len(container_word)
best_index = i
ans.append(best_index)
return ans