FastPrepFastPrep
Problem Brief

Suffix Pairs

OA

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].

1Example 1

Input
words = ["hack", "hackhour", "jammon", "backgammon", "comeback", "come", "door"]
Output
3
Explanation

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"

2Example 2

Input
words = ["cha", "a", "a", "b", "ba", "ca"]
Output
8
Explanation

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

Limits and guarantees your solution can rely on.

  • 1 ≤ words.length ≤ 105
  • 1 ≤ words[i].length ≤ 10
function solution(words: string[]): number {
    // write your code here
}
Input

words

["hack", "hackhour", "jammon", "backgammon", "comeback", "come", "door"]

Output

3

Sign in to submit your solution.