Optimize chained strings matching algorithm