String pattern matching algorithm