# Tag Info

## Hot answers tagged substrings

8

Yes, there is a more efficient algorithm. Your algorithm can take exponential time. You can check whether there exists any match in $O(nm)$ time, where $n$ is the length of text and $m$ is the length of mask, and find all matches in $O(n^2m)$ time. I'll show two solutions, one using dynamic programming and one using graph search. You can pick whichever ...

7

Hendrik Jan is correct about the Knuth-Morris-Pratt algorithm (warning, wikipedia doesn't explain it particularly well, a text on algorithms is probably a better bet). The failure function can be used to extract a DFA that can perform the string matching. It's not immediately obvious that the failure table is a DFA, but with very little work the transition ...

7

Create a temporary source string by concatenating itself together until the length of the source string is at least twice the length of the search string. The source string must be concatenated at least once. Then perform a simple (non-circular) search on that temporary string.

6

Here is the clean presentation (after going in circles around it). First you consider all substrings of S that are in M*. From that you build a trie, which may be understood as a tree structured FA that recognizes these substrings. You build it so that you complete first the transitions of nodes that are closest to the root. As soon as you have a node from ...

6

You can build a suffix tree in linear time and check if there's an inner node that corresponds to a full string (constant time per node). In more detail, assume we are given strings $s_1, \dots, s_n \in \Sigma^*$. Build a (generalised) suffix tree of $s_1\$_1, s_2\$_2, \dots, s_n\$_n$with$n$pairwise distinct terminal markers$\$_1,\dots,\$_n \notin \...

5

Given a FA $M$ accepting a language $L$, a suffix automaton is one which accepts the language of all suffixes of strings in $L$. In other words for a language $L$ over an alphabet $\Sigma$, $$\operatorname{SUFFIX}(L)=\{y\mid\exists x\in\Sigma^* \ (xy\in L)\}$$ For example, if $L$ is the language denoted by the regex $ab^*a$ we'll have $L=\{aa, aba, abba, ... 5 Say that$F(n)$occurs at some position if the substring starting at that position is compatible with either$F(n)$or its complementation. How close can occurrences of$F(n)$be? Take as an example$F(4) = abaab$. If$F(4)$occurs at position$p$then it cannot occur at position$p+1$or$p+2$, but it can appear at position$p+3$. We let$\ell(n)$be the ... 5 The problem is called substring search or string search or string matching. It's called a substring even if the elements of the array aren't characters. In this answer, I'll use character to refer to elements of the arrays, but this can be any kind of data (numbers, nested data structures, …). The standard terminology is: subset (without multiplicity): all ... 5 Answer. Although I have seen a deleted answer, I still think the construction is linear in the length of the string (considering the alphabet size constant). Aho Corasick 1975, doi:10.1145/360825.360855 Or perhaps KMP. Aho & Corasick consider pattern matching automata for a finite set of words, forming a tree with back-pointers in case of mismatches=... 5 Using Yuval's reduction, the problem can be reformulated as "Find the largest range$(i,j)$in a binary string$S \in \{0,1\}^*$such that the number of$1$s contained in the range is$\leq K$" (see this question). The idea is to start with an initial valid range$(0,j)$of length$maxlen$and then shift the "window"$(i,i+maxlen)$to the right, checking at ... 5 One can reduce your problem to the following. Given a sequence of$N$numbers, find a contiguous subsequence of length at most$K+1$having maximal sum. This problem, in turn, is solvable in time$O(N)$. What's the connection between your problem and mine? Let the positions of the mistakes be$I_1,\ldots,I_t$, and add$I_0 = 0$,$I_{t+1} = N+1$. The ... 5 Suffix arrays can be used for this problem. They contain the starting positions of each suffix of the string sorted in lexicographic order. Even though they can be constructed naively in$O(n\log n)$complexity, there are methods to construct them in$\Theta(n)$complexity. See for example this and this. Let us call this suffix array SA. Once the suffix ... 5 This problem is called shortest superstring problem. John Gallant, David Maier and James Astorer proved it is NP-hard in 19791. Given two strings$A$and$B$, let$|A|$denote the length of$A$, and let$S(A,B)$denote the shortest superstring of$A$and$B$where$A$occurs before$B$. It is easy to reduce this problem to travelling salesman problem, ... 5 A trie is asymptotically optimal for this. No data structure can achieve better asymptotic running time. If you care about constant factors, the only way to know what will be optimal is to try multiple approaches and benchmark them. Standard theoretical running time analysis is not reliable at predicting constant factors. Another data structure to ... 5 Suppose the alphabet is$\{a,b\}$, and you have one forbidden word,$aa$. Suppose we are trying to generate a word of length 3. The first two letters will be distributed uniformly over$ab,ba,bb$. Hence the first letter has the following distribution:$a$with probability$1/3$,$b$with probability$2/3$. In contrast, the allowable words are$$aba,abb,bab,... 5 This can be solved with Aho-Corasick algorithm in$O(nm + Mm)$time, where$M$is the number of pairs outputted. First build the Aho-Corasick automaton for the set of strings in$O(nm)$time. Then run each string through the automaton - this takes$O(nm)$time for running the strings through the automaton and$O(Mm)$time for outputting the matches because ... 4 Below is an expected$\mathcal{O}(n + m )$algorithm (which can be extended to other$k$, making it$\mathcal{O}(nk +m )$). (I haven't done the calculations to prove it is so, though). The idea is similar to the Rabin-Karp rolling hash algorithm for exact substring matches. The idea is to separate each string of length$m$into$2k$blocks of$m/2k$size ... 4 Sedgewick continues: "These are pattern characters that we know, so we can ﬁgure out ahead of time, for each possible mismatch position, the state where we need to restart the DFA." What I understand is that this quote tries to explain that if you encounter a mismatch at letter at position$j$of the pattern you have to shift the patterns by one position ... 4 You can construct a suffix tree for that very large file and once generated the same can be used for querying. For Suffix tree generation use Suffix Array approach, there are many algorithm to generate suffix array. This is a taxonomy of many suffix array algorithm available today http://www.cas.mcmaster.ca/~bill/best/algorithms/07Taxonomy.pdf To starts ... 4 Construct the suffix tree of your string, which takes time linear in the length of the string (assuming a finite alphabet). Every inner node represents a repeat, their respective descendant leaves encode the positions. 4 The answer is 66: any sequence of length greater than 66 must contain some repeated substring (as you argue in the question), and there exists a sequence of length 66 where no substring is repeated. The latter can be obtained from a de Bruijn sequence with$n=3$and$k=4$. The length of this sequence is$k^n=64$symbols. A de Bruijn sequence is a cyclic ... 4 I don't think this is possible. Changing a high-order digit in a base-10 number -- say, changing 5000000 to 6000000 -- can change bits in its binary equivalent all the way from high-order bits to fairly-low-order bits: 10011000100101101000000 10110111000110110000000 It doesn't change the very lowest bits: Since$10 = 5\times 2$, the digit in position$k$... 4 The DP suggested in the comments by Yuval Filmus indeed works: we can solve the problem in$\mathcal{O}(n^{2})$by DP. First note that we may assume that intervals are not allowed to overlap. Take any two overlapping intervals$[a, b)$and$[c, d)$. WLOG$a \leq c$. Then we can replace the two intervals with the intervals$[a, c)$and$[\min(b, d), \max(b, ...

3

Instead of a formal proof, I want to give some intuition behind the formula. The suffix array contains all the suffixes of the string $w$. A substring is nothing else than a prefix of a suffix. So if you count $\sum_i |S[i]|$, you will get all the substrings, but of course you overcount the number of different substrings. Let's have a closer look. Assume ...

3

Checking whether two 32-bit integers are equal can be done in a single machine instruction. Checking whether two strings are equal can take a lot longer. If my two strings are each 100 characters long, then checking whether those two strings are equal might require the equivalent of about 100 machine instructions: I have to check them, character by ...

3

Here's another approach that takes only linear time. Construct a suffix tree for $S$ (and alphabet $M$). Perform a breadth-first traversal and stop at the first missing edge or non-singleton label $ab\dots$ (if there is no missing edge on that level). The path from the root to (and including the) missing edge resp. $ax$ with $x \neq b$ is your result. This ...

3

Well these patterns will make KMP work faster: T=aaaaaaaaaa P=aaaa KMP will try 10 compare steps were Boyer-Moore will take 28 Another example: T=aaaaaaaaaa P=abab KMP will try 8 compare steps where BM will try 12.

3

There is a paper that did a good experiment over these string matching algorithms for different patterns: "Comparison of string matching algorithms: an aid to information content security" Also there is a study of these string matching algorithms for Japanese language: Comparison and Improvement of String Matching Algorithms For Japanese Texts I hope these ...

3

You are probably looking for the term n-grams, with $n = 2$ in your case. This is very useful in natural language processing and speech recognition.

3

It looks like you're missing the overlap case, if I understand your diagram correctly. This does the trick: A // goto B if read 0 else stay B // goto C if read 1 else stay C // goto D if read 1 else goto B D // goto E if read 0 else goto A E // emit rising edge, goto B assuming that you have a falling edge after every read.

Only top voted, non community-wiki answers of a minimum length are eligible