Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

14. Longest Common Prefix

Easy44.3% Acceptance
StringTrie
Asked by:
F
Facebook
Apple
ProblemSolutions (12)VideosCompanies (10)Notes

Problem Statement

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".

Example 1:

Input: strs = ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Constraints:

  • 1 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • strs[i] consists of only lowercase English letters.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
A
G
Google
A
Adobe
A
Amazon
+5

Approach

The Longest Common Prefix problem asks you to find the longest starting substring shared by all strings in an array. A common way to think about this is by comparing characters position by position across all strings until a mismatch appears.

One intuitive method is horizontal scanning. Start with the first string as a candidate prefix and progressively compare it with each subsequent string, shrinking the prefix whenever characters stop matching. This works efficiently because the prefix only gets shorter as comparisons continue.

Another useful idea is sorting the array and comparing only the first and last strings. Since sorting places lexicographically similar strings close together, the common prefix of these two extremes reflects the prefix shared by the whole list.

For learning purposes, you can also model the strings using a Trie (prefix tree). By inserting all words and traversing nodes that have only one child, you can determine the shared prefix structure. Typical solutions run in O(N * M) time where N is the number of strings and M is the average string length.

Complexity

ApproachTime ComplexitySpace Complexity
Horizontal ScanningO(N * M)O(1)
Sorting + First/Last ComparisonO(N log N * M)O(1)
Trie (Prefix Tree)O(N * M)O(N * M)

Video Solution Available

NeetCode

View all video solutions

Solutions (12)

Horizontal Scanning

In this approach, you start with the first string as a reference and gradually compare it with each subsequent string in the array. The reference prefix is shortened until it matches the prefixes of all strings.

Time Complexity: O(S), where S is the sum of all characters in all strings.
Space Complexity: O(1), as we are using constant extra space.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <string.h>
3
4char* longestCommonPrefix(char** strs, int strsSize) {
5    if (strsSize == 0) return "";
6    char* prefix = strs[0];
7    for (int i = 1; i < strsSize; i++) {
8        while (strstr(strs[i], prefix) != strs[i]) {
9            prefix[strlen(prefix) - 1] = '\0';
10            if (*prefix == '\0') return "";
11        }
12    }
13    return prefix;
14}
15
16int main() {
17    char *strs[] = { "flower", "flow", "flight" };
18    printf("%s\n", longestCommonPrefix(strs, 3));
19    return 0;
20}

Explanation

This implementation starts by taking the first string as the initial prefix. It iteratively checks if this prefix is valid for each string, adjusting it until all strings have a common start. If a mismatch occurs, we shorten the prefix from the end.

Divide and Conquer

This approach involves dividing the array of strings into two halves, recursively finding the longest common prefix for each half, and merging the results. The merge step compares characters from the two strings to find the common prefix.

Time Complexity: O(S), where S is the sum of all characters in the strings.
Space Complexity: O(M*logN), where M is the length of the common prefix and N is the number of strings.

CC++JavaPythonC#JavaScript
1#



Video Solutions

Watch expert explanations and walkthroughs

Longest Common Prefix - Leetcode 14 - Python

NeetCode
6:31217,791 views

Asked By Companies

10 companies
F
Facebook
A
Apple
G
Google
A
Adobe
A
Amazon
M
Microsoft
B
Bloomberg
U
Uber
Q
Quora
S
SAP

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Word BreakMedium
Word Break IIHard
Implement Trie (Prefix Tree)Medium
Design Add and Search Words Data StructureMedium
More similar problems

Related Topics

StringTrie

Problem Stats

Acceptance Rate44.3%
DifficultyEasy
Companies10

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Longest Common Prefix asked in FAANG interviews?

Yes, Longest Common Prefix is a common introductory string problem asked in technical interviews, including FAANG-style interviews. It tests understanding of string manipulation, edge cases, and basic algorithm design.

What data structure is best for Longest Common Prefix?

A Trie (prefix tree) is a natural data structure for prefix-related problems. By inserting all strings and traversing nodes that have only one child, you can identify the shared prefix among all words. However, for interview settings, simpler string comparison approaches are often preferred.

What is the optimal approach for Longest Common Prefix?

A common optimal approach is horizontal scanning. Start with the first string as the prefix and compare it with each subsequent string, trimming the prefix whenever characters differ. This keeps the solution simple and runs in O(N * M) time where N is the number of strings and M is the prefix length.

Can sorting help solve the Longest Common Prefix problem?

Yes, sorting the array of strings can simplify the problem. After sorting, you only need to compare the first and last strings because they will represent the maximum possible difference in prefixes. Their shared prefix will match the common prefix of the entire array.

Previous Problem

Roman to Integer

Next Problem

Letter Combinations of a Phone Number

include
<stdio.h>
2
#
include
<string.h>
3
4
char
*
commonPrefix
(
char
*
left
,
char
*
right
)
{
5
int
minLength
=
strlen
(
left
)
<
strlen
(
right
)
?
strlen
(
left
)
:
strlen
(
right
)
;
6
for
(
int
i
=
0
;
i
<
minLength
;
i
++
)
{
7
if
(
left
[
i
]
!=
right
[
i
]
)
{
8
left
[
i
]
=
'\0'
;
9
break
;
10
}
11
}
12
return
left
;
13
}
14
15
char
*
divideAndConquer
(
char
*
*
strs
,
int
left
,
int
right
)
{
16
if
(
left
==
right
)
{
17
return
strs
[
left
]
;
18
}
else
{
19
int
mid
=
(
left
+
right
)
/
2
;
20
char
*
lcpLeft
=
divideAndConquer
(
strs
,
left
,
mid
)
;
21
char
*
lcpRight
=
divideAndConquer
(
strs
,
mid
+
1
,
right
)
;
22
return
commonPrefix
(
lcpLeft
,
lcpRight
)
;
23
}
24
}
25
26
char
*
longestCommonPrefix
(
char
*
*
strs
,
int
strsSize
)
{
27
if
(
strsSize
==
0
)
return
""
;
28
return
divideAndConquer
(
strs
,
0
,
strsSize
-
1
)
;
29
}
30
31
int
main
(
)
{
32
char
*
strs
[
]
=
{
"flower"
,
"flow"
,
"flight"
}
;
33
printf
(
"%s\n"
,
longestCommonPrefix
(
strs
,
3
)
)
;
34
return
0
;
35
}

Explanation

This C solution reduces the problem into finding the longest common prefix of smaller sections, leading to a combination of results using a helper function to merge prefixes by character comparison.