PythonMediumStrings20 min
Find the longest common prefix
Strings contain lowercase English letters and may be empty. With zero strings, return an empty string. Interviewers use this problem to assess use the shortest word as the maximum possible prefix.
Input
First line: n. Next n lines: one string per line.
Output
Print the result on one line; print an empty line for an empty string.
Constraints
0 <= n <= 10000; total characters <= 100000.
Example
Input
3 flower flow flight
Output
fl
Example 2 input
3 same same same
Output
same
Identical words share their entire text.
Public test cases (2)
Input
3 flower flow flight
Expected output
fl
Input
3 dog racecar car
Expected output
—
Hints (2)
- Identify the state needed to avoid repeated work.
- Use the shortest word as the maximum possible prefix.
Target complexity: time O(S), where S is total input characters · space O(1) excluding returned prefix
