Python Program To Find Longest Common Prefix Using Sorting Last Updated : 24 Mar, 2023 Summarize Comments Improve Suggest changes Share Like Article Like 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. Word by Word MatchingCharacter by Character MatchingDivide and ConquerBinary Search.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 OutputThe 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! Comment More infoAdvertise with us Next Article Python Program To Find Longest Common Prefix Using Word By Word Matching K kartik Follow Improve Article Tags : Strings Sorting Python Programs DSA Longest Common Prefix +1 More Practice Tags : SortingStrings Similar Reads Python Program To Find Longest Common Prefix Using Word By Word Matching Given a set of strings, find the longest common prefix. Examples: Input : {âgeeksforgeeksâ, âgeeksâ, âgeekâ, âgeezerâ} Output : "gee" Input : {"apple", "ape", "april"} Output : "ap"Recommended: Please solve it on âPRACTICE â first, before moving on to the solution. We start with an example. Suppose 4 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 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 Find Length Of The Longest Substring Without Repeating Characters Given a string str, find the length of the longest substring without repeating characters. For âABDEFGABEFâ, the longest substring are âBDEFGAâ and "DEFGAB", with length 6.For âBBBBâ the longest substring is âBâ, with length 1.For "GEEKSFORGEEKS", there are two longest substrings shown in the below 6 min read Python program to find Indices of Overlapping Substrings To count the number of overlapping sub-strings in Python we can use the Re module. To get the indices we will use the re.finditer() method. But it returns the count of non-overlapping indices only. Examples: Input: String: "geeksforgeeksforgeeks" ; Pattern: "geeksforgeeks" Output: [0, 8] Explanation 4 min read Like