>but I think this solution is not efficient.
Why? If you're just guessing or using your "programmer's intuition", you're probably wrong. Interviewers (good ones) tend to smile more on people who aren't afraid to say "I'd rather optimize clean, correct code than fix fast code". That's the sign of an experienced programmer.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>I don't know the answer of the second question:
>if pattern is a array, how to optimize it?
The answer is the same.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
if the pattern matching is done on a very long sequence of characters (eg. searching for the occurrence of a particular phrase or word in a large book), there are several interesting algorithms. here is a link that explores some of them. http://www.jea.acm.org/ARTICLES/Vol4Nbr2/index.html
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287