Problem · String
Suffix Pairs
Learn this problemProblem 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[]) → longExamples
Example 1
words = ["back", "backdoor", "gammon", "backgammon", "comeback", "come", "door"]return = 3The relevant pairs are:
words[0] = "back"andwords[4] = "comeback"words[1] = "backdoor"andwords[6] = "door"words[2] = "gammon"andwords[3] = "backgammon"
Example 2
words = ["cba", "a", "a", "b", "ba", "ca"]return = 8The relevant pairs are:
words[0] = "cba"andwords[1] = "a"words[0] = "cba"andwords[2] = "a"words[0] = "cba"andwords[4] = "ba"words[1] = "a"andwords[2] = "a"words[1] = "a"andwords[4] = "ba"words[1] = "a"andwords[5] = "ca"words[2] = "a"andwords[4] = "ba"words[2] = "a"andwords[5] = "ca"
Constraints
1 ≤ words.length ≤ 1051 ≤ words[i].length ≤ 10