Given an array of strings arr[], the task is to find the pair of strings from the given array whose length of the longest common prefix between them is maximum. If multiple solutions exist, then print any one of them.
Examples:
Input: arr[] = {“geeksforgeeks”, “geeks”, “geeksforcse”, }
Output: (geeksforgeeks, geeksforcse)
Explanation:
All possible pairs and their longest common prefix are:
(“geeksforgeeks”, “geeks”) has the longest common prefix = “geeks”
(“geeksforgeeks”, “geeksforcse”) has the longest common prefix = “geeksfor”
(“geeks”, “geeksforcse”) has the longest common prefix = “geeks”
Therefore, a pair having maximum length of the longest common prefix is (“geeksforgeeks”, “geeksforcse”)Input: arr[] = {“abbcbgfh”, “bcdee”, “bcde”, “abbcbde”}
Output: (abbcbgfh, abbcbde)
Naive Approach: The simplest approach to solve this problem is to generate all possible pairs of the given array and calculate the length of the longest common prefix of each pair. Finally, print the pair having a maximum length of the longest common prefix.
Time Complexity: O(N2 * M), Where M denotes the length of the longest string
Auxiliary Space: O(1)
Efficient Approach: The problem can be solved using Trie. The idea is to traverse the given array and for each array element, find the maximum length of the longest prefix present in Trie, and insert the current element into the Trie. Finally, print the pair having a maximum length of the longest common prefix. Follow the steps below to solve the problem:
- Create a Trie having root node, say root to store each element of the given array.
- Traverse the given array and for each array element, find the maximum length of the longest prefix present in Trie and insert the current element into Trie.
- Finally, print the pair having a maximum length of the longest common prefix.
Below is the implementation of the above approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Structure of Trie struct TrieNode { // Stores characters of // each string TrieNode* child[256]; TrieNode() { child[0] = child[1] = NULL; } }; // Function to insert a string into Trie void insertTrie(TrieNode* root, string str) { // Stores length of the string int M = str.length(); // Traverse the string str for (int i = 0; i < M; i++) { // If str[i] is not present // in current path of Trie if (!root->child[str[i]]) { // Create a new node // of Trie root->child[str[i]] = new TrieNode(); } // Update root root = root->child[str[i]]; } } // Function to find the maximum length of // longest common prefix in Trie with str int findStrLen(TrieNode* root, string str) { // Stores length of str int M = str.length(); // Stores length of longest // common prefix in Trie with str int len = 0; // Traverse the string str for (int i = 0; i < M; i++) { // If str[i] is present in // the current path of Trie if (root->child[str[i]]) { // Update len len++; // Update root root = root->child[str[i]]; } else { return len; } } return len; } // Function to print the pair having maximum // length of the longest common prefix void findMaxLenPair(vector<string>& arr, int N) { // Stores index of the string having // maximum length of longest common prefix int idx = -1; // Stores maximum length of longest // common prefix. int len = 0; // Create root node of Trie TrieNode* root = new TrieNode(); // Insert arr[0] into Trie insertTrie(root, arr[0]); // Traverse the array. for (int i = 1; i < N; i++) { // Stores maximum length of longest // common prefix in Trie with arr[i] int temp = findStrLen(root, arr[i]); // If temp is greater than len if (temp > len) { // Update len len = temp; // Update idx idx = i; } insertTrie(root, arr[i]); } // Traverse array arr[] for (int i = 0; i < N; i++) { // Stores length of arr[i] int M = arr[i].length(); // Check if maximum length of // longest common prefix > M if (i != idx && M >= len) { // Traverse string arr[i] // and arr[j] for (int j = 0; j < len; j++) { // If current character of both // string does not match. if (arr[i][j] != arr[idx][j]) { break; } } // Print pairs having maximum length // of the longest common prefix cout << "(" << arr[i] << ", " << arr[idx] << ")"; return; } } } // Driver Code int main() { vector<string> arr = { "geeksforgeeks", "geeks", "geeksforcse" }; int N = arr.size(); findMaxLenPair(arr, N); } |
(geeksforgeeks, geeksforcse)
Time Complexity: O(N * M), where M denotes the length of the longest string
Auxiliary Space: 0(N * 256)
Recommended Posts:
- Construct an Array of Strings having Longest Common Prefix specified by the given Array
- Length of longest common prefix possible by rearranging strings in a given array
- Find the longest common prefix between two strings after performing swaps on second string
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Maximum sum increasing subsequence from a prefix and a given element after prefix is must
- Longest Common Prefix using Word by Word Matching
- Longest Common Prefix using Character by Character Matching
- Longest Common Prefix using Divide and Conquer Algorithm
- Longest Common Prefix using Binary Search
- Longest Common Prefix using Trie
- Longest Common Prefix using Sorting
- Find minimum shift for longest common prefix
- Longest Common Prefix using Linked List
- Longest Common Prefix Matching | Set-6
- Count of strings whose prefix match with the given string to a given length k
- Longest subsequence having maximum GCD between any pair of distinct elements
- Longest string which is prefix string of at least two strings
- Longest Common Substring in an Array of Strings
- Length of longest balanced parentheses prefix
- Pair of integers having least GCD among all given pairs having GCD exceeding K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


