Location-Aware Matching Statistics
Abstract
Edit distance is the standard measure of string similarity, but its quadratic-time complexity is prohibitive for long strings. Popular linear-time alternatives, such as the $q$-gram distance, or the average common substring measure (ACS) that is based on matching statistics (MS), consider the substrings shared by the two input strings but not where they occur in each string. Yet in many real-world applications, e.g., when comparing document versions or co-recorded time series, a shared substring is really relevant only if its occurrences are close in position. We introduce Location-Aware Matching Statistics (LAMAS). Given strings $X$ and $Y$ and a displacement tolerance $d$, LAMAS reports, for every position $i$ of $Y$, the longest substring starting at $i$ that occurs in $X$ at a position $j$ with $|j-i|\le d$, together with the displacement $(j-i)$. MS is the special case of LAMAS with unbounded $d$. Our main result is an algorithm that solves LAMAS in $\mathcal{O}(n)$ time and space for strings of length at most $n$ and any $d$; this is optimal and matches the complexity of MS. The algorithm reduces LAMAS to predecessor and successor queries over a sliding window of suffix ranks and answers them in linear total time via a window decomposition and interval union-find. Experiments on 20 real datasets and synthetic data show that LAMAS substantially improves over MS and $q$-gram distance in similarity-based classification, top-$K$ retrieval, and clustering. It is competitive with edit distance and a constrained dynamic time warping variant in classification and generally outperforms a state-of-the-art method for top-$K$ retrieval under edit distance. Finally, our implementation of the main algorithm for LAMAS scales linearly in $n$ and does not slow down as $d$ grows.