Suppose I have a set of strings S and a query string q. I want to know if any member of S is a substring of q. (For the purposes of this question, a substring includes equality, for example, “foo” - a substring of “foo”.) For example, suppose a function that does what I need is called anySubstring
:
S = ["foo", "baz"] q = "foobar" assert anySubstring(S, q) # "foo" is a substring of "foobar" S = ["waldo", "baz"] assert not anySubstring(S, q)
Is there any easy-to-implement algorithm for this with sublinear time complexity in len(S)
? This is normal if S must first be processed in some smart data structure, because I will query each S with a large number of rows q, so the amortized cost of this preprocessing can be reasonable.
EDIT: To clarify, I don't care which member S is a substring of q, only if at least one of them. In other words, I only care about the logical answer.
source share