VOOZH about

URL: https://www.geeksforgeeks.org/dsa/longest-common-prefix/

⇱ Longest Common Prefix - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Longest Common Prefix

Last Updated : 21 Apr, 2026

Given an array of strings arr[], return the longest common prefix among each and every strings present in the array. If there’s no prefix common in all the strings, return “”.

Input: arr[] = [“geeksforgeeks”, “geeks”, “geek”, “geezer”]
Output: “gee”
Explanation: “gee” is the longest common prefix in all the given strings: “geeksforgeeks”, “geeks”, “geeks” and “geezer”.

Input: arr[] = [“apple”, “ape”, “april”]
Output : “ap”
Explanation: “ap” is the longest common prefix in all the given strings: “apple”, “ape” and “april”.

Input: arr[] = [“hello”, “world”]
Output: “”
Explanation: There’s no common prefix in the given strings.

[Naive Approach] Using Sorting - O(n*m*log n)Time O(m)Space

The idea is to sort the array of strings and find the common prefix of the first and last string of the sorted array. Sorting is used in this approach because it makes it easier to find the longest common prefix. When we sort the strings, the first and last strings in the sorted list will be the most different from each other in terms of their characters. So, the longest common prefix for all the strings must be a prefix of both the first and the last strings in the sorted list.

Algorithm:

  • Given array of strings is [“geeksforgeeks”, “geeks”, “geek”, “geezer”].
  • After sorting it becomes [“geek” ,”geeks” ,”geeksforgeeks” ,”geezer”].
  • Now, to find the longest common prefix, we only need to compare the first and last strings (“geek” and “geezer“) because any common prefix between these two will also be a prefix for all the strings in between.
  • In this case, the common prefix between “geek” and “geezer” is “gee“, which is the longest common prefix for all the strings.

Output
gee

Time Complexity: O(n*m*log n), to sort the array, where n is the number of strings and m is the length of longest string.
Auxiliary Space: O(m) to store the strings first, last and result.

[Expected Approach 1] Character by Character Matching - O(n * m) Time and O(m) Space

The idea is to iterate character by character from index 0 and take the current character from the first string as reference. For each position, we compare this character with the corresponding character in all other strings. If all characters match, we add it to the result; otherwise, we stop immediately and return the prefix formed so far.

Algorithm:

  • Start with the first character and one by one compare each character in all strings
  • For each comparison, match characters until a mismatch and keep only the common part.
  • After all comparisons, the matched prefix is the longest common prefix.

Output
gee

[Expected Approach 2] Using Divide and Conquer Algorithm - O(n*m) Time O(m) Space

The idea is simple, first divide the array of strings into two equal parts. Then find the Longest Common Prefix for all strings in each part individually using recursion. Once we got the Longest Common Prefix of both parts, the Longest Common Prefix of this array will be Longest Common Prefix of these two parts.

Algorithm:

  • Divide the array of strings into two halves recursively.
  • If only one string remains, return it as the prefix.
  • Recursively find the LCP of left half (p1) and right half (p2).
  • Compare p1 and p2 character by character.
  • Return their common prefix.
  • Final result is the LCP of the whole array.
👁 longest_common_prefix_using_divide_and_conquer_algorithm

Output
gee

[Expected Approach 3] Using Trie - O(n*m) Time O(n*m) Space

The idea is to insert all the string one by one in the trie. After inserting we perform a walk on the trie. In this walk, we go deeper until we find a node having more than 1 children(branching occurs) or 0 children (one of the string gets exhausted). This is because the characters (nodes in trie) which are present in the longest common prefix must be the single child of its parent, i.e- there should not be branching in any of these nodes.

Algorithm:

  • Create a Trie data structure.
  • Insert all strings into the Trie.
  • Start traversing from the root node.
  • At each step:
    • If the node has exactly one child and is not end of a word, continue.
    • Otherwise, stop traversal.
  • Collect characters during traversal → this forms the LCP.
  • Return the collected prefix.
👁 longest_common_prefix_using_trie

Output
gee
Comment
Article Tags: