The Longest Common Extension (LCE) problem considers a string s and computes, for each pair (L , R), the longest sub string of s that starts at both L and R. In LCE, in each of the query we have to answer the length of the longest common prefix starting at indexes L and R.
String : “abbababba”
Queries: LCE(1, 2), LCE(1, 6) and LCE(0, 5)
Find the length of the Longest Common Prefix starting at index given as, (1, 2), (1, 6) and (0, 5).
The string highlighted “green” are the longest common prefix starting at index- L and R of the respective queries. We have to find the length of the longest common prefix starting at index- (1, 2), (1, 6) and (0, 5).
Algorithm (Naive Method)
- For each of the LCE queries of the form – LCE(L, R) do the following:
- Initialise the LCE ‘length’ as 0
- Start comparing the prefix starting from index- L and R character by character.
- If the characters matches, then this character is in our Longest Common Extension. So increment ‘length’ (length++).
- Else if the characters mismatch, then return this ‘length’.
- The returned ‘length’ will be the required LCE(L, R).
Below is C++ implementation of above Naive algorithm.
LCE(1, 2) = 1 LCE(1, 6) = 3 LCE(0, 5) = 4
Time Complexity: The time complexity is O(Q.N), where
Q = Number of LCE Queries
N = Length of the input string
One may be surprised that the although having a greater asymptotic time complexity, the naive method outperforms other efficient method(asymptotically) in practical uses. We will be discussing this in coming sets on this topic.
Auxiliary Space: O(1), in-place algorithm.
- K-Mismatch Problem->Landau-Vishkin uses LCE as a subroutine to solve k-mismatch problem
- Approximate String Searching.
- Palindrome Matching with Wildcards.
- K-Difference Global Alignment.
In the next sets we will discuss how LCE (Longest Common Extension) problem can be reduced to a RMQ (Range Minimum Query). We will also discuss more efficient methods to find the longest common extension.
This article is contributed by Rachit Belwariar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Longest Common Extension / LCE | Set 3 (Segment Tree Method)
- Longest Common Extension / LCE | Set 2 ( Reduction to RMQ)
- Longest Common Substring | DP-29
- Longest Common Subsequence | DP-4
- Longest Common Prefix Matching | Set-6
- Longest Common Anagram Subsequence
- Longest Common Subsequence | DP using Memoization
- Longest Common Prefix using Trie
- LCS (Longest Common Subsequence) of three strings
- Printing Longest Common Subsequence
- Print the longest common substring
- Longest Common Prefix using Sorting
- SequenceMatcher in Python for Longest Common Substring
- Longest common subsequence with permutations allowed
- Edit distance and LCS (Longest Common Subsequence)