# Check if multiple strings exist in another string

Posted on

Solving problem is about exposing yourself to as many situations as possible like Check if multiple strings exist in another string and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about Check if multiple strings exist in another string, which can be followed any time. Take easy to follow this discuss.

Check if multiple strings exist in another string

How can I check if any of the strings in an array exists in another string?

Like:

``````a = ['a', 'b', 'c']
str = "a123"
if a in str:
print "some of the strings found in str"
else:
print "no strings found in str"
``````

That code doesn’t work, it’s just to show what I want to achieve.

You can use `any`:

``````a_string = "A string is more than its parts!"
matches = ["more", "wholesome", "milk"]
if any(x in a_string for x in matches):
``````

Similarly to check if all the strings from the list are found, use `all` instead of `any`.

`any()` is by far the best approach if all you want is `True` or `False`, but if you want to know specifically which string/strings match, you can use a couple things.

If you want the first match (with `False` as a default):

``````match = next((x for x in a if x in str), False)
``````

If you want to get all matches (including duplicates):

``````matches = [x for x in a if x in str]
``````

If you want to get all non-duplicate matches (disregarding order):

``````matches = {x for x in a if x in str}
``````

If you want to get all non-duplicate matches in the right order:

``````matches = []
for x in a:
if x in str and x not in matches:
matches.append(x)
``````

You should be careful if the strings in `a` or `str` gets longer. The straightforward solutions take O(S*(A^2)), where `S` is the length of `str` and A is the sum of the lenghts of all strings in `a`. For a faster solution, look at Aho-Corasick algorithm for string matching, which runs in linear time O(S+A).

Just to add some diversity with `regex`:

``````import re
if any(re.findall(r'a|b|c', str, re.IGNORECASE)):
print 'possible matches thanks to regex'
else:
print 'no matches'
``````

or if your list is too long – `any(re.findall(r'|'.join(a), str, re.IGNORECASE))`

You need to iterate on the elements of a.

``````a = ['a', 'b', 'c']
str = "a123"
found_a_string = False
for item in a:
if item in str:
found_a_string = True
if found_a_string:
print "found a match"
else:
print "no match found"
``````

A surprisingly fast approach is to use `set`:

``````a = ['a', 'b', 'c']
str = "a123"
if set(a) & set(str):
print("some of the strings found in str")
else:
print("no strings found in str")
``````

This works if `a` does not contain any multiple-character values (in which case use `any` as listed above). If so, it’s simpler to specify `a` as a string: `a = 'abc'`.

Here is one way to use it in Python:

2. Put it in the same directory as your main Python file and name it `aho_corasick.py`

3. Try the alrorithm with the following code:

``````from aho_corasick import aho_corasick #(string, keywords)
print(aho_corasick(string, ["keyword1", "keyword2"]))
``````

Note that the search is case-sensitive

``````a = ['a', 'b', 'c']