Open In App

Python Program To Find Longest Common Prefix Using Sorting

Last Updated : 24 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Problem Statement: Given a set of strings, find the longest common prefix.
Examples: 

Input: {"geeksforgeeks", "geeks", "geek", "geezer"}
Output: "gee"

Input: {"apple", "ape", "april"}
Output: "ap"

The longest common prefix for an array of strings is the common prefix between 2 most dissimilar strings. For example, in the given array {“apple”, “ape”, “zebra”}, there is no common prefix because the 2 most dissimilar strings of the array “ape” and “zebra” do not share any starting characters. 
We have discussed five different approaches in below posts.  

  1. Word by Word Matching
  2. Character by Character Matching
  3. Divide and Conquer
  4. Binary Search.
  5. Using Trie) 

In this post a new method based on sorting is discussed. The idea is to sort the array of strings and find the common prefix of the first and last string of the sorted array.

Python 3




# Python 3 program to find longest
# common prefix of given array of words.
def longestCommonPrefix( a):
     
    size = len(a)
 
    # if size is 0, return empty string
    if (size == 0):
        return ""
 
    if (size == 1):
        return a[0]
 
    # sort the array of strings
    a.sort()
     
    # find the minimum length from
    # first and last string
    end = min(len(a[0]), len(a[size - 1]))
 
    # find the common prefix between
    # the first and last string
    i = 0
    while (i < end and
           a[0][i] == a[size - 1][i]):
        i += 1
 
    pre = a[0][0: i]
    return pre
 
# Driver Code
if __name__ == "__main__":
 
    input = ["geeksforgeeks", "geeks",
                     "geek", "geezer"]
    print("The longest Common Prefix is :" ,
                 longestCommonPrefix(input))
 
# This code is contributed by ita_c


Output

The longest Common Prefix is : gee

Time Complexity: O(MAX * n * log n ) where n is the number of strings in the array and MAX is maximum number of characters in any string. Please note that comparison of two strings would take at most O(MAX) time and for sorting n strings, we would need O(MAX * n * log n ) time. 

Space Complexity: O(1) as no extra space has been used.

Please refer complete article on Longest Common Prefix using Sorting for more details!



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads