FastPrepCount Equal or Suffix Word Pairs

Count Equal or Suffix Word Pairs

Hudson River Trading logoHudson River Trading● MediumFULLTIMEOA

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[]) → long

Examples

Example 1

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

The 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 = 8

The 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 <= 100000
  • 1 <= words[i].length <= 10
  • Every word contains only lowercase English letters.

More Hudson River Trading problems

See Hudson River Trading hiring insights
Saved
public long countSuffixPairs(String[] words) {
    // Return the number of matching index pairs.
}
Checking account…