Sum of all LCP of maximum length by selecting any two Strings at a time

• Last Updated : 19 Jul, 2021

Given a list of strings, the task is to find the sum of all LCP (Longest Common Prefix) of maximum length by selecting any two strings at a time.
Examples:

Input: str[] = {babab, ababb, abbab, aaaaa, babaa, babbb}
Output:
Explanation:
Choose 1st and 5th string => length of LCP = 4,
Choose 2nd and 3rd string => length of LCP = 2
Sum of LCP = 4 + 2 = 6 Input: str = [“aa”, “aaaa”, “aaaaaaaa”, “aaaabaaaa”, “aaabaaa”]
Output:
Explanation:
Choose 3rd (aaaaaaaa) and 4th string (aaaabaaaa) => length of LCP (aaaa) = 4,
Choose 2nd (aaaa) and 5th (aaabaaa) string => length of LCP (aaa) = 3
Sum of LCP = 4 + 3 = 7

Naive Approach:

• Sort the list of strings in decreasing order of their length
• Then take the first string from the list and find the Longest Common Prefix with all other remaining string in the list and store it in the array
• Choose the maximum value from the array and add it to variable answer and remove the pair of string from the list corresponding to that sum
• Repeat the above procedures for all the next strings till the list is empty or you reach the last string
• The variable answer has the required sum of all LCP of maximum length

Time Complexity: O(M*N2), where M = maximum string length and N = number of strings.
Efficient Approach:
An efficient solution can be obtained using a Trie Data Structure. To find the number of characters common between the strings we will use the variable ‘visited’ to keep track of how many times one character is visited.
Following are the steps:

• Insert list of string in trie such that every string in the list is inserted as an individual trie node.

• For all prefixes of maximum length, count the pairs from deepest node in the trie.

• Use depth-first search (DFS) traversal on trie to count the pairs from deepest node.

• If the value of visited node is more than one, it means that there two or more strings that have common prefix up till that node.

• Add the value of that visited node to a variable count.

• Decrease the value of that visited node from current and previous nodes such that the pair of words chosen for calculation must be removed.

• Repeat the above steps for all nodes and return the value of count.

Below is the implementation of the above approach:

Javascript


Output:
6

Time Complexity:
For inserting all the strings in the trie: O(MN)
For performing trie traversal: O(26*M) ~ O(M)
Therefore, overall Time complexity: O(M*N), where:

N = Number of strings
M = Length of the largest string

Auxiliary Space: O(M)

My Personal Notes arrow_drop_up