FastPrepCount Prefix-Related Word Pairs

Count Prefix-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 prefix of the other.

Equal words qualify.

Function

countPrefixPairs(words: String[]) → long

Examples

Example 1

words = ["a","ab","abc","b"]return = 3

The qualifying pairs are (a, ab), (a, abc), and (ab, abc).

Example 2

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

The two equal words qualify, and each is a prefix of xy.

Constraints

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

More ZipRecruiter problems

See ZipRecruiter hiring insights
public long countPrefixPairs(String[] words) {
    // Write your code here.
}
words["a","ab","abc","b"]
expected3
Checking account…