191. Count Prefix and Suffix Pairs

EasyStringString

You are given a list of strings (comma-separated). A pair (i, j) is counted if i < j and words[i] is both a prefix and a suffix of words[j].

Return the total count of such valid pairs.

Input: Comma-separated list of strings.

Output: An integer — the count of valid (i,j) pairs.

Examples

Example 1
Input: a,aba,ababa,aa
Output: 4
Explanation: Valid pairs: (a,aba),(a,ababa),(a,aa),(aba,ababa).
Example 2
Input: pa,papa,mamama,ma
Output: 2
Explanation: (pa,papa) and (ma,mamama) are valid.
Example 3
Input: abab,ab
Output: 0
Explanation: i<j required; 'ab'(j=1) doesn't include 'abab'(i=0) as prefix+suffix.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →