![]() |
VOOZH | about |
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.
Example:
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)
Implementation :
Below is C++ implementation of above Naive algorithm.
LCE (1, 2) = 1 LCE (1, 6) = 3 LCE (0, 5) = 4
Analysis of Naive Method :
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.
Applications:
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.