Search code examples
algorithmstring-matchingknuth-morris-pratt

Handling wildcard '*' operator in string matching using KMP-Algorithm?


How should I handle the case when the pattern to be matched contains the wildcard character, *, such as AB*C, which is present is the text, ABEFGCS (here * consumes characters EFG) using the KMP-Algorithm ?

What modification in the algorithm can solve this problem ?


Solution

  • Actually I got it, leaving answer for reference, we can simply break the string about the wildcard operator, apply KMP on each part, and check whether each part is a sub-string or not, also, whether the parts are contiguous or not can be checked in linear time, hence the overall time complexity is still linear.