RobloxOA #15
Suffix Pairs
估计中等评测数据准备中,可先阅读题面
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].
Constraints
1 ≤ words.length ≤ 10⁵1 ≤ words[i].length ≤ 10
Example 1
Input:
words = ["hack", "hackhour", "jammon", "backgammon", "comeback", "come", "door"]
Output:
3
Explanation: The relevant pairs are:
words[0] = "hack"andwords[1] = "comeback"words[1] = "hackhour"andwords[6] = "door"words[2] = "jammon"andwords[3] = "backgammon"
Example 2
Input:
words = ["cha", "a", "a", "b", "ba", "ca"]
Output:
8
Explanation: The relevant pairs are:
words[0] = "cha"andwords[1] = "a"words[0] = "cha"andwords[2] = "a"words[0] = "cha"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"