A router groups incoming URL route segments (each a string of lowercase English letters) and wants to know how much of a leading path they all share. Compute the length of the longest common prefix of all n given segments: the largest L such that the first L characters of every segment are identical. If the segments share no leading character, the answer is 0.
Line 1: an integer n, the number of segments.
Next n lines: one segment per line, each a non-empty string of lowercase English letters.
A single integer: the length of the longest common prefix of all n segments.
a-z).Example 1
Input
3 flower flow flight
Expected
2
Explanation
All three start with 'fl' but differ at the third character, so the common prefix length is 2.
Example 2
Input
2 dog cat
Expected
0
Explanation
The two segments differ at the very first character, so the common prefix length is 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →