Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Longest Common Prefix Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Longest Common Prefix | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement 

Given a list of strings A, find the longest common prefix among all the strings.

Naive Approach

A simple solution is to traverse the list and calculate the longest common prefix of each string with the longest common prefix found so far.

Analysis

  • Time Complexity: O(n * (Max String Size))
  • Auxiliary Space Complexity: O(1)

Implementation

C++
string longestCommonPrefix(vector<string> str) {
   string lcp = str[0];
   for (int i = 1; i < str.size(); i++) {
       string commonPrefix;
       for (int j = 0; j < min (lcp.size(), str[i].size()); j++) {
           if (lcp[j] == str[i][j]) {
               commonPrefix += lcp[j];
           } else {
               break;
           }
       }
       lcp = commonPrefix;
   }
   return lcp;
}
Java
class Solution {
   String longestCommonPrefix(String[] str) {
       String lcp = str[0];
       for (int i = 1; i < str.length; i++) {
           String commonPrefix  = "";
           for (int j = 0; j < Math.min(lcp.length(), str[i].length()); j++) {
               if (lcp.charAt(j) == str[i].charAt(j)) {
                   commonPrefix += lcp.charAt(j);
               } else {
                   break;
               }
           }
           lcp = commonPrefix;
       }
       return lcp;
   }
}

Optimal Approach

The optimal and better approach for this problem is to sort the given strings and compare the first and last string to find the longest common prefix. The reason behind checking the first and last strings only is that all the strings in-between must have the same prefix as the strings are sorted lexicographically. 

Analysis

  • Time Complexity: O(n * logn + (Max String Size))
  • Auxiliary Space Complexity: O(1)

Implementation

C++
string longestCommonPrefix(vector<string> str) {
   sort (str.begin(), str.end());
   int n = str.size() - 1;
   string lcp;
   for (int i = 0; i < min (str[0].size(), str[n].size()); i++) {
       if (str[0][i] == str[n][i]) {
           lcp += str[0][i];
       } else {
           break;
       }
   }
   return lcp;
}
Java
class Solution {
   String longestCommonPrefix(String[] str) {
       Arrays.sort (str);
       int n = str.length - 1;
       String lcp = "";
       for (int i = 0; i < Math.min (str[0].length(), str[n].length()); i++) {
           if (str[0].charAt(i) == str[n].charAt(i)) {
               lcp += str[0].charAt(i);
           } else {
               break;
           }
       }
       return lcp;
   }
}
Related Content
Anagrams
Compare Version Numbers
Count And Say
Integer to Roman Numeral
Longest Palindrome in String
Insert Minimum To Make Palindrome
Reverse Words in String
Roman Numeral to Integer
Substring Search - I
Substring Search - III
Substring Search - II
Substring Search - IV
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet