Count Equal or Suffix Word Pairs
Problem statement
You are given an array of strings words. Count the index pairs (i, j) with i < j for which either word is a suffix of the other.
A suffix is a string that matches the end of another string. Equal strings are suffixes of each other, and their index pair counts once. Repeated strings at different indices can form different pairs.
Return the number of matching pairs.
Function
countSuffixPairs(words: String[]) → longExamples
Example 1
words = ["back","backdoor","gammon","backgammon","comeback","come","door"]return = 3The matching index pairs are (0, 4), (1, 6) and (2, 3). They match through the suffixes back, door and gammon.
Example 2
words = ["cba","a","a","b","ba","ca"]return = 8The matching index pairs are (0, 1), (0, 2), (0, 4), (1, 2), (1, 4), (1, 5), (2, 4) and (2, 5). The two copies of a count as separate indices.
Constraints
1 <= words.length <= 1000001 <= words[i].length <= 10- Every word contains only lowercase English letters.