Consider a scenario where you need to efficiently find all occurrences of a relatively short pattern within a long text document. Which pattern matching algorithm would be most suitable, and why?

  • Boyer-Moore Algorithm
  • Knuth-Morris-Pratt (KMP) Algorithm
  • Naive Pattern Matching
  • Rabin-Karp Algorithm
In this scenario, the Knuth-Morris-Pratt (KMP) algorithm would be most suitable. KMP is efficient for finding all occurrences of a short pattern in a long text document without unnecessary backtracking, as it preprocesses the pattern to avoid redundant comparisons.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *