in reply to Re: Better way to search array for elements in other array?
in thread Better way to search array for elements in other array?

This turns your algorithm from O(N * M) to O(N).

Not exactly. Suppose that L is the length of the strings you are trying to match. With the naive approach the RE engine can take advantage of Boyer-Moore, so its running time should be about O(N * M / L). With the trie approach the number of times that the re engine will start trying to match is O(N), however each attempted match that fails can cost up to O(L), so your running time is potentially as high as O(N * L). (Usually you won't hit this worst case though.)

If you have a lot of strings to match, this is potentially a big win. If you have a small number of long strings to match, this is probably not a good optimization.

  • Comment on Re^2: Better way to search array for elements in other array?

Replies are listed 'Best First'.
Re^3: Better way to search array for elements in other array?
by Limbic~Region (Chancellor) on Jan 24, 2011 at 20:20 UTC
    tilly,
    I was very careful through out my post to say things like "may" and "to see if it fits". I should have said of the algorithm analysis....if you don't look too closely, this turns your algorithm.... I appreciate the clarification. I have other ideas about how this could be optimized for run time but the OP didn't provide enough real sample data.

    Cheers - L~R