Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast algorithm for finding out if a string contains any string in a given array

I have a list of about 50 keywords and about 50000 strings. I check every string if it contains at least one of the keywords. I'm not interested in the matched keyword or the number of matched keywords. I only want a "true" or "false" back, as fast as possible.

So, I bet there's an algorithm out there that outperforms my current LINQ version by far:

class MyEnumerableExtension
{
    public static bool ContainsAny(this string searchString, IEnumerable<string> keywords)
    {
        return keywords.Any(keyword => searchString.Contains(keyword))
    }
}

bool foundAny = "abcdef".ContainsAny(new string[] { "ac", "bd", "cd" } );
like image 721
VVS Avatar asked Jun 10 '26 05:06

VVS


1 Answers

isn't this in essence the same as your other question of today Efficient algorithm for finding all keywords in a text except modified to return once a match has been found?

like image 99
The Archetypal Paul Avatar answered Jun 13 '26 18:06

The Archetypal Paul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!