Problem · String

Suffix Pairs

Learn this problem
MediumRoblox logoRobloxOA
See Roblox hiring insights

Problem statement

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

Function

solution(words: String[]) → long

Examples

Example 1

words = ["back", "backdoor", "gammon", "backgammon", "comeback", "come", "door"]return = 3

The relevant pairs are:

  1. words[0] = "back" and words[4] = "comeback"
  2. words[1] = "backdoor" and words[6] = "door"
  3. words[2] = "gammon" and words[3] = "backgammon"

Example 2

words = ["cba", "a", "a", "b", "ba", "ca"]return = 8

The relevant pairs are:

  1. words[0] = "cba" and words[1] = "a"
  2. words[0] = "cba" and words[2] = "a"
  3. words[0] = "cba" 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
public long solution(String[] words) {
  // write your code here
}
words["back", "backdoor", "gammon", "backgammon", "comeback", "come", "door"]
expected3
checking account