C Program
#include<stdio.h> #include<string.h> int main() { char a[100][100]; int n; scanf("%d", &n); for(int i=0; i<n; i++) scanf("%s", a[i]); int i=0; while(a[0][i]) for(int j=1; j<n; j++) if(a[j][i] != a[0][i]) goto end; i++; end: for(int k=0; k<i; k++) putchar(a[0][k]); }
C Output
Input: 3 flower flow flight Output: fl
C++ Program
#include<iostream> using namespace std; int main() { string a[100]; int n; cin >> n; for(int i=0; i<n; i++) cin >> a[i]; string res = ""; for(int i=0; i<a[0].size(); i++) { char c = a[0][i]; for(int j=1; j<n; j++) if(i >= a[j].size() || a[j][i] != c) goto end; res += c; } end: cout << res; }
C++ Output
Input: 4 interview interstate internal internet Output: inte
JAVA Program
import java.util.*; class LCP { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String[] a = new String[n]; for(int i=0; i<n; i++) a[i] = sc.next(); String res = ""; for(int i=0; i<a[0].length(); i++) { char c = a[0].charAt(i); for(int j=1; j<n; j++) if(i >= a[j].length() || a[j].charAt(i) != c) { System.out.print(res); return; } res += c; } System.out.print(res); } }
JAVA Output
Input: 3 class classy classic Output: class
Python Program
a = input().split() res = "" for i in range(len(a[0])): for s in a: if i >= len(s) or s[i] != a[0][i]: print(res); exit() res += a[0][i] print(res)
Python Output
Input: cool coo co Output: co
In-Depth Learning – Whole Concept in Paragraphs
What Is the Longest Common Prefix?
The Longest Common Prefix (LCP) is the longest prefix substring (beginning from the start) common to all strings in a list. It should be at the front of every string and should be identical for all of them.
For instance, from ["flow", "flower", "flight"], the common prefix substring is "fl" — it is the LCP.
Instruction:
How the Code Works
We take the first string as a reference and compare each of its characters with all other strings, position by position.
If:
The character is found in all strings → we add it to the result.
A mismatch occurs or a string reaches its end → we exit (early exit).
This approach is quick for short strings and easy to read for beginners. It never sorts and doesn't rely on any libraries — purely raw character comparison.
The approach employs:
A loop over characters in the first string.
A nested loop over all other strings.
On finding a character mismatch or length overflow, we return the constructed prefix.
Example
Input:
["class", "classy", "classic"]
Steps:
See if all begin with 'c' → yes
See next 'l' → yes
Next 'a' → yes
Next 's' → yes
Next 's' → yes
Next char: 'y' vs 'i' → mismatch
Output:
"class"
Real-Life Analogy
Imagine autocomplete on a search engine. When users begin to type "pro", the engine attempts to identify the longest matching prefix from the list of available keywords such as "program", "project", "profile", etc. The longest matching portion they all have is the common prefix, which is used to suggest relevant results.
For example, family tree names — individuals belonging to the same family can have the same initial surname or tag, which is essentially a common prefix for everyone.
Where and When Is It Used?
LCP is particularly useful in:
Autocomplete engines
Search box suggestions
Data compression (trie trees)
DNA sequence analysis
Detection of common patterns in logs or strings
It is often presented during coding rounds by recruiters from Google, Amazon, TCS, Wipro, and Infosys.
Time and Space Complexity
Operation\tComplexity
Time O(n × m)
Space O(1) (if not using additional data structures)
Where n is number of strings and m is average length of the prefix.
Trie Trees or Divide and Conquer versions are used in optimized versions, but for small inputs, the character-by-character method is quicker and easier.
Python Tip (1-liner version)
python
import os
print(os.path.commonprefix(input().split()))
Note: Works only for prefixes, not general substrings.
SEO-Optimized Natural Paragraph for Ranking
Want to implement longest common prefix (LCP) in a list of strings in C, C++, Java, or Python? This tutorial demonstrates how to get the common prefix among multiple strings using basic and effective character comparison logic. LCP is frequently encountered in string-based interviews, autocomplete systems, search optimizers, and DNA matching software. With step-by-step examples, in-place code, and easy-to-understand logic, this tutorial leads you through implementing an LCP finder with no fancy libraries or data structures. Learn LCP logic and implement it in actual projects and competitive programming competitions with ease.
Social Plugin