Tap here to switch tabs
Problem
Ask
Submissions

Problem: Longest Common Suffix Queries

hard
40 min
Explore how to efficiently identify the string with the longest common suffix from a container array for each query string. This lesson teaches using trie data structures to optimize suffix matching under constraints, focusing on selection criteria such as suffix length, string length, and occurrence order. Practice implementing this approach with hands-on coding.

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

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

  • wordsContainer[i] consists only of lowercase English letters.

  • wordsQuery[i] consists only of lowercase English letters.

  • Sum of wordsContainer[i].length is, at most 51055 * 10^5.

  • Sum of wordsQuery[i].length is, at most 51055 * 10^5.

Tap here to switch tabs
Problem
Ask
Submissions

Problem: Longest Common Suffix Queries

hard
40 min
Explore how to efficiently identify the string with the longest common suffix from a container array for each query string. This lesson teaches using trie data structures to optimize suffix matching under constraints, focusing on selection criteria such as suffix length, string length, and occurrence order. Practice implementing this approach with hands-on coding.

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

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

  • wordsContainer[i] consists only of lowercase English letters.

  • wordsQuery[i] consists only of lowercase English letters.

  • Sum of wordsContainer[i].length is, at most 51055 * 10^5.

  • Sum of wordsQuery[i].length is, at most 51055 * 10^5.