Problem 19
Longest Common Prefix
Easy · 10 pts · String
Problem
You are given N strings of lowercase letters. Find their longest common prefix: the longest string that every one of them starts with. If the strings do not even share a first letter, the common prefix is empty, and you should print an empty line. If there is only one string, the whole string is the answer. One approach is to compare the strings column by column: check the first letter of every string, then the second, and stop at the first column where they disagree or a string ends.
Input
First line: integer N. The next N lines each contain one lowercase string.
Output
Print the longest common prefix, or an empty line if none exists.
Constraints
1 <= N <= 1000; 1 <= length of each string <= 1000
Example input
3 flower flow flight
Example output
fl
Explanation
The strings are flower, flow and flight. All three start with 'f' and then 'l', but at the third position they have 'o', 'o' and 'i', which differ. So the longest common prefix is fl.