advanced
Big-O Optimization Challenge
Optimize a theoretical inefficient algorithm to reduce time complexity.
📝 Prompt Content
Given a naive implementation of a string matching algorithm with O(n*m) complexity, analyze the algorithmic bottlenecks. Refactor the logic to implement the Knuth-Morris-Pratt (KMP) algorithm or the Boyer-Moore algorithm. Provide a step-by-step explanation of the prefix function (or failure function) construction and prove how it reduces the worst-case time complexity to linear time.