Solve the substring pattern matching problem.