Capital OneOA #9
Book Title Catalog — Count Titles That Are Prefix of Another
Estimated MediumJudge-ready
Topics (AI-labelled)Trie & CodecStrings
Given a list of book titles titles, count the ordered index pairs (i,j) such that i≠j and titles[i] is a prefix of titles[j]. Identical titles at different indices also count as prefixes.
Input
The first line contains n (1≤n≤200000), followed by n lines, each a non-empty title consisting only of lowercase English letters, with length 1..1000; the total length of all titles is ≤2×10^6. The character set and constraints are site supplements.
Output
Output the number of ordered index pairs that satisfy the condition.
Example 1
Input:
6
wall
wallpaper
science
wallet
philosophy
phil
Output:
3
Example 2
Input:
1
a
Output:
0
Example 3
Input:
4
ab
ab
abc
b
Output:
4
Editorial
Loading solution…