Problem · String

Suffix Pairs

MediumRobloxOA
See Roblox hiring insights

Given an array of strings words, find the number of pairs where either the strings are equal or one string ends with another. In other words, find the number of such pairs (i, j) (0 ≤ i < j < words.length) that words[i] is a suffix of words[j].

Examples
01 · Example 1
words = ["hack", "hackhour", "jammon", "backgammon", "comeback", "come", "door"]
return = 3

The relevant pairs are:

  1. words[0] = "hack" and words[1] = "comeback"
  2. words[1] = "hackhour" and words[6] = "door"
  3. words[2] = "jammon" and words[3] = "backgammon"

02 · Example 2
words = ["cha", "a", "a", "b", "ba", "ca"]
return = 8

The relevant pairs are:

  1. words[0] = "cha" and words[1] = "a"
  2. words[0] = "cha" and words[2] = "a"
  3. words[0] = "cha" and words[4] = "ba"
  4. words[1] = "a" and words[2] = "a"
  5. words[1] = "a" and words[4] = "ba"
  6. words[1] = "a" and words[5] = "ca"
  7. words[2] = "a" and words[4] = "ba"
  8. words[2] = "a" and words[5] = "ca"

Constraints
  • 1 ≤ words.length ≤ 105
  • 1 ≤ words[i].length ≤ 10
More Roblox problems
drafts saved locally
function solution(words: string[]): number {
    // write your code here
}
words["hack", "hackhour", "jammon", "backgammon", "comeback", "come", "door"]
expected3
sign in to submit