Linear Time Subsequence and Supersequence Regex Matching
arXiv:2504.16288 · doi:10.4230/LIPIcs.MFCS.2025.9
Abstract
It is well-known that checking whether a given string matches a given regular expression can be done in quadratic time and that this cannot be improved to a truly subquadratic running time of assuming the strong exponential time hypothesis (SETH). We study the related problem that asks whether has a \emph{subsequence} that matches , and we show that surprisingly this task admits an algorithm that runs in linear time, i.e., in . We further show that the same holds if we ask for a supersequence instead of a subsequence. Moreover, we show that the \emph{quantitative} problems of computing a longest subsequence or shortest supersequence of that matches can be solved with the same complexity as the classical longest common subsequence or shortest common supersequence problems, i.e., in , and conditionally not in . By contrast, if instead of subsequences or supersequences we consider other string relations like the infix, prefix, left-extension, or extension relations, then all the corresponding problems (both quantitative and non-quantitative) have the same complexity as classical regex matching, i.e., they can also be solved in , but not in assuming SETH. We last study the complexity of the \emph{universal} problem that asks if \emph{all} subsequences (or supersequences, infixes, prefixes, left-extensions or extensions) of an input string satisfy a given regular expression. For these problems, we show polynomial upper bounds (along with matching conditional lower bounds) for the infix and prefix relations, but PSPACE-completeness for the extension, left-extension and supersequence relations, and coNP-completeness for the subsequence relation.
33 pages. Full version including all proofs