Advanced
Algorithm Optimization
Optimize a naive string search algorithm description and analyze complexity.
📝 Contenuto del prompt
Given a naive string matching algorithm that compares a pattern to every substring in a text, propose an optimized version using the Knuth-Morris-Pratt algorithm. Explain the preprocessing step for the partial match table and demonstrate how it reduces the time complexity from O(nm) to O(n+m). Provide a pseudocode implementation.