Python Program To Find Longest Common Prefix Using Word By Word Matching
Last Updated :
19 Mar, 2023
Given a set of strings, find the longest common prefix.
Examples:
Input : {“geeksforgeeks”, “geeks”, “geek”, “geezer”}
Output : "gee"
Input : {"apple", "ape", "april"}
Output : "ap"
We start with an example. Suppose there are two strings- “geeksforgeeks” and “geeks”. What is the longest common prefix in both of them? It is “geeks”.
Now let us introduce another word “geek”. So now what is the longest common prefix in these three words ? It is “geek”
We can see that the longest common prefix holds the associative property, i.e-
LCP(string1, string2, string3)
= LCP (LCP (string1, string2), string3)
Like here
LCP (“geeksforgeeks”, “geeks”, “geek”)
= LCP (LCP (“geeksforgeeks”, “geeks”), “geek”)
= LCP (“geeks”, “geek”) = “geek”
So we can make use of the above associative property to find the LCP of the given strings. We one by one calculate the LCP of each of the given string with the LCP so far. The final result will be our longest common prefix of all the strings.
Note that it is possible that the given strings have no common prefix. This happens when the first character of all the strings are not same.
We show the algorithm with the input strings- “geeksforgeeks”, “geeks”, “geek”, “geezer” by the below figure.

Below is the implementation of above approach:
Python3
# Python3 Program to find the longest
# common prefix
# A Utility Function to find the common
# prefix between strings- str1 and str2
def commonPrefixUtil(str1, str2):
result = "";
n1 = len(str1)
n2 = len(str2)
# Compare str1 and str2
i = 0
j = 0
while i <= n1 - 1 and j <= n2 - 1:
if (str1[i] != str2[j]):
break
result += str1[i]
i += 1
j += 1
return (result)
# A Function that returns the longest
# common prefix from the array of strings
def commonPrefix (arr, n):
prefix = arr[0]
for i in range (1, n):
prefix = commonPrefixUtil(prefix,
arr[i])
return (prefix)
# Driver Code
if __name__ =="__main__":
arr = ["geeksforgeeks", "geeks",
"geek", "geezer"]
n = len(arr)
ans = commonPrefix(arr, n)
if (len(ans)):
print ("The longest common prefix is -",
ans);
else:
print("There is no common prefix")
# This code is contributed by ita_c
OutputThe longest common prefix is - gee
Time Complexity : Since we are iterating through all the strings and for each string we are iterating though each characters, so we can say that the time complexity is O(N M) where,
N = Number of strings
M = Length of the largest string string
Auxiliary Space : To store the longest prefix string we are allocating space which is O(M). Please refer complete article on Longest Common Prefix using Word by Word Matching for more details!
Method #2:
In this approach, the first word is taken as the longest common prefix, and then each subsequent word is compared to the prefix character by character. The prefix is updated as needed so that it only includes characters that are common to all the words. This process continues until all the words have been compared and the longest common prefix has been found.
Approach:
- Define the function find_longest_common_prefix that takes a list of words as input.
- Initialize the variable longest_common_prefix to the first word in the list of words.
- Loop through each word in the list of words, starting from the second word.
- Loop through each character in the current longest common prefix.
- If the current character is not the same as the character in the same position in the current word, update the longest_common_prefix variable to the substring of the longest common prefix up to the index of the current character and break out of the loop.
- If the loop completes without breaking, the longest_common_prefix variable will be equal to the shortest word in the list of words.
- Return the longest_common_prefix variable.
Python3
def find_longest_common_prefix(words):
# Initialize the longest common prefix to the first word
longest_common_prefix = words[0]
# Loop through each word in the list of words
for word in words[1:]:
# Loop through each character in the current longest common prefix
for i in range(len(longest_common_prefix)):
# If the current character is not the same as the character in the same position in the current word
if i >= len(word) or longest_common_prefix[i] != word[i]:
# Update the longest common prefix and break out of the loop
longest_common_prefix = longest_common_prefix[:i]
break
# Return the longest common prefix
return longest_common_prefix
words = ["geeksforgeeks", "geeks", "geek", "geezer"]
longest_common_prefix = find_longest_common_prefix(words)
print("The longest common prefix is:", longest_common_prefix)
OutputThe longest common prefix is: gee
Overall, the function has a time complexity of O(mn)
Auxiliary space: O(1), where m is the length of the longest word and n is the number of words in the list.
Similar Reads
Python Program To Find Longest Common Prefix Using Sorting
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
2 min read
Python Program to Check Overlapping Prefix - Suffix in Two Lists
Given 2 Strings, our task is to check overlapping of one string's suffix with prefix of other string. Input : test_str1 = "Gfgisbest", test_str2 = "bestforall" Output : best Explanation : best overlaps as suffix of first string and prefix of next. Input : test_str1 = "Gfgisbest", test_str2 = "restfo
4 min read
Python program to Concatenate Kth index words of String
Given a string with words, concatenate the Kth index of each word. Input : test_str = 'geeksforgeeks best geeks', K = 3 Output : ktk Explanation : 3rd index of "geeksforgeeks" is k, "best" has 't' as 3rd element. Input : test_str = 'geeksforgeeks best geeks', K = 0 Output : gbg Method #1 : Using joi
4 min read
Python program to find start and end indices of all Words in a String
Given a String, return all the start indices and end indices of each word. Examples: Input : test_str = ' Geekforgeeks is Best' Output : [(1, 12), (16, 17), (19, 22)] Explanation : "Best" Starts at 19th index, and ends at 22nd index. Input : test_str = ' Geekforgeeks is Best' Output : [(1, 12), (17,
4 min read
Python program to find the longest word in a sentence
In this article, we will explore various methods to find the longest word in a sentence.Using LoopFirst split the sentence into words using split() and then uses a loop (for loop) to iterate through the words and keeps track of the longest word by comparing their lengths.Pythons = "I am learning Pyt
1 min read
Python program to remove K length words in String
Given a String, write a Python program to remove all the words with K length. Examples: Input : test_str = 'Gfg is best for all geeks', K = 3 Output : is best geeks Explanation : Gfg, for and all are of length 3, hence removed. Input : test_str = 'Gfg is best for all geeks', K = 2 Output : Gfg best
5 min read
Python Program to Return the Length of the Longest Word from the List of Words
When working with lists of words in Python, we may need to determine the length of the longest word in the list. For example, given a list like ["Python", "with", "GFG], we would want to find that "Python" has the longest length. Let's go through some methods to achieve this.Using max() max() functi
3 min read
Python Program for Longest Common Subsequence
LCS Problem Statement: Given two sequences, find the length of longest subsequence present in both of them. A subsequence is a sequence that appears in the same relative order, but not necessarily contiguous. For example, "abc", "abg", "bdf", "aeg", '"acefg", .. etc are subsequences of "abcdefg". So
3 min read
Python - Ways to determine common prefix in set of strings
A common prefix is the longest substring that appears at the beginning of all strings in a set. Common prefixes in a set of strings can be determined using methods like os.path.commonprefix() for quick results, itertools.takewhile() combined with zip() for a more flexible approach, or iterative comp
2 min read
Python Program to find the Larger String without Using Built-in Functions
Given two strings. The task is to find the larger string without using built-in functions. Examples: Input: GeeksforGeeks Geeks Output: GeeksforGeeks Input: GeeksForFeeks is an good Computer Coding Website It offers several topics Output: GeeksForFeeks is an good Computer Coding Website Step-by-step
3 min read