Wildcard Matching

Implement wildcard pattern matching with support for '?' and '*'. 

'?' Matches any single character. 
'*' Matches any sequence of characters (including the empty sequence). 

The matching should cover the entire input string (not partial). 
The function prototype should be: bool isMatch(const char *s, const char *p) 

Some examples: 
isMatch("aa","a") → false 
isMatch("aa","aa") → true 
isMatch("aaa","aa") → false 
isMatch("aa", "*") → true 
isMatch("aa", "a*") → true 
isMatch("ab", "?*") → true 
isMatch("aab", "c*a*b") → false

Use two pointers to save the position of last '*' and the position of corresponding char in s. First time when we meet '*', we assume it is an empty string. Once we do not find the match, backtrack to the last '*', and assume it matches a string of length 1. Continuously doing this until we reach the end of the string.

Comments

  1. Can you please give an example for each case ?
    we are stuck up with else if conditions. We are not able to analyze the solution. I would really be grateful if you explain with example

    ReplyDelete

Post a Comment

Popular posts from this blog

Maximum Gap

[ITint5] Maximum Subarray for a Circular Array

[CC150] Chapter 4 Trees and Graphs