FastPrepCount Suffix-Related Word Pairs

Count Suffix-Related Word Pairs

ZipRecruiter logoZipRecruiter● EasyNEW GRADOA
Learn

Problem statement

You are given an array of strings words. Count unordered index pairs (i, j) with i < j for which either word is a suffix of the other.

Equal words qualify.

Function

countSuffixPairs(words: String[]) → long

Examples

Example 1

words = ["a","ba","cba","x"]return = 3

a is a suffix of ba and cba, and ba is a suffix of cba.

Example 2

words = ["x","x","yx"]return = 3

The equal x values qualify, and each is a suffix of yx.

Constraints

  • 0 <= words.length <= 2000
  • 0 <= words[i].length <= 1000
  • Words contain lowercase English letters.

More ZipRecruiter problems

See ZipRecruiter hiring insights
public long countSuffixPairs(String[] words) {
    // Write your code here.
}
words["a","ba","cba","x"]
expected3
Checking account…