Given an array of strings arr[], the task is to 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 “”.
Examples:
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.
Using Sorting
In this method, we sort the array of strings. After sorting, the longest common prefix of the whole array will be the common prefix between the first and last strings in the sorted array. 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.
To know more about the implementation, please refer Longest Common Prefix using Sorting.
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.
Using Word by Word Matching
The idea here is based on the associative property of the Longest Common Prefix (LCP). Specifically:
LCP (“geeksforgeeks”, “geeks”, “geek”)
= LCP (LCP (“geeksforgeeks”, “geeks”), “geek”)
= LCP (“geeks”, “geek”)
= “geek”
Using this property, we calculate the LCP of each string with the current LCP found so far. By the end, this results in the LCP of the entire array of strings.
To know more about the implementation, please refer Longest Common Prefix Word by Word Matching.
Time Complexity: O(n*m), where n is number of strings and m is length of the longest string.
Auxiliary Space: O(m), to store the longest prefix string.
Using Character by Character Matching
This approach is similar to word-by-word matching, but instead, we compare characters across the strings one at a time. First, we compare the character at the 0th index, then the 1st index, and so on until all characters at an index are the same.
This method stops as soon as a mismatch is found, making it efficient if the common prefix is short or doesn’t exist at all. Word-by-word matching, on the other hand, goes through each word entirely, even if there’s no common prefix.
To know more about the implementation, please refer Longest Common Prefix Character by Character Matching.
Time Complexity: O(n*m), where n is number of strings and m is length of the shortest string.
Auxiliary Space: O(m), to store the longest prefix string.
Using Divide and Conquer
The divide and conquer method splits the array of strings into two parts. Each part’s Longest Common Prefix is computed recursively, and then the Longest Common Prefix of the two parts is combined to get the Longest Common Prefix of the entire array. This approach is similar to the divide-and-conquer technique used in merge sort.
To know more about the implementation, please refer Longest Common Prefix Divide and Conquer.
Time Complexity: O(n*m), where n is number of strings and m is length of the longest string.
Auxiliary Space: O(m), to store the longest prefix string.
Using Binary Search
The idea is to use binary search on the indices to find the length of longest common prefix.
We can observe that the length of common prefix cannot be greater than the length of smallest string in given set of strings. So the search space for binary search will be from 0 to length of smallest string – 1.
In each iteration of binary search [low to high], we will check whether the prefix [0 to mid] is common among all strings or not.
- If the prefix is common among all string then check for larger length prefix by updating low = mid + 1
- Else check for smaller length prefix, by updating high = mid – 1.
To know more about the implementation, please refer Longest Common Prefix Binary Search.
Time Complexity: O(n*m), where n is number of strings and m is length of shortest string.
Auxiliary Space: O(n), to store the longest prefix string.
Using Trie
In this approach, each string is inserted into a trie, character by character. After insertion, we traverse the trie until a branching node (one with more than one child) or an end node is found. 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.
To know more about the implementation, please refer Longest Common Prefix Using Trie.
Time Complexity: O(n*m), where n is the number of strings and m is the length of the longest string.
Auxiliary Space: O(n*m), to store all the strings in Trie.