Dsa
Strings
Easy
Common Prefixes in Strings
Given a list of strings, write a program that finds the longest common prefix shared among all strings in the list. If there is no common prefix, return an empty string.
Input:
The first line of input contains an integer n (1 ≤ n ≤ 100), the number of strings. The next n lines each contain a single string s (1 ≤ |s| ≤ 100), consisting of lowercase letters only.
Output:
Print the longest common prefix among all strings.
Example:
Input:
3
flower
flow
flight
Output:
fl
Key concepts
stringsprefixessearching
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now