Longest Common Prefix
Practice this coding question on Coding Gurukul.
Problem statement
A document room groups folders by the common beginning of their labels. The clerk wants the longest label fragment that appears at the beginning of every folder name, so that this fragment can become the heading on a shared shelf. Your task Given n lowercase strings, return their longest common prefix. A prefix begins at character zero and must match every string character for character. Important details The answer may be an entire shortest string or may be empty. An empty label forces an empty answer. A shared fragment occurring only in the middle is not a prefix. Keep the comparison case-sensitive. Worked example Labels orchard, orbit and orange all begin with or. Their third letters differ, so the shared heading is or and cannot be extended.
Input format
The first line contains n. Each of the next n lines is one string, possibly empty. Read these as lines.
Output format
Print the prefix, or EMPTY if it is empty.
Constraints
1 <= n <= 200; each string has 0 to 200 lowercase English letters.
Sample input
3 orchard orbit orange
Sample output
or
Sign in to run or submit a solution in C++, Java or Python.