LinkedInOA #6
Longest String Chain
Estimated MediumJudge-ready
Topics (AI-labelled)Tree & Game DPHashing
Choose a word chain from the word list, where each next word is obtained from the previous one by inserting exactly one letter while keeping the order of the other letters. Find the number of words in the longest chain. Words of the same length cannot be predecessors of each other.
Input
The first line contains the number of words n, followed by one word per line. Original bounds 1≤n≤1000, each word has length 1..16, all lowercase English; duplicate words are allowed.
Output
Output a single integer answer.
Example 1
Input:
6
a
b
ba
bca
bda
bdca
Output:
4
Example 2
Input:
5
xbc
pcxbcf
xb
cxbc
pcxbc
Output:
5
Example 3
Input:
2
abcd
dbqca
Output:
1
Example notes
The outputs of the three public examples are, in order: 4; 5; 1. Checked by independent enumeration or direct simulation.
Editorial
Loading solution…