Skip to content

Practice Count Prefix and Suffix Pairs II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3045
LeetCode Problem

Count Prefix and Suffix Pairs II

Problem Statement

Given words, count pairs of indices (i, j) with i < j for which words[i] is both a prefix and a suffix of words[j]. A prefix begins at the first character and a suffix ends at the last character; they may overlap. Thus aba qualifies for ababa, whereas abc does not qualify for abcd. Return the pair count.

Source: Count Prefix and Suffix Pairs II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
words = ["a","aba","ababa","aa"]
Output
4

# Example 2

Input
words = ["pa","papa","ma","mama"]
Output
2

# Example 3

Input
words = ["abab","ab"]
Output
0

Constraints

1 <= words.length <= 10^5
1 <= words[i].length <= 10^5
words[i] consists only of lowercase English letters.
The sum of the lengths of all words[i] does not exceed 5 * 10^5.

Related LeetCode Problems

Practice more problems similar to Count Prefix and Suffix Pairs II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.