Likou brushing notes: 44. Wildcard matching

Topic : 44. Wildcard Matching
Given a string (s) and a character pattern§, implement a wildcard matching that supports'?' and'*'.

'?' can match any single character.
'*' can match any string (including empty strings).
Only when two strings match exactly is the match successful.

Description:

s may be empty and only contain lowercase letters from az.
p may be empty and only contain lowercase letters from az, as well as the characters? and *.
Example 1:

Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" cannot match the entire string of "aa".

Idea : Problem solution from Likou boss, plus my own understanding

  • This is a problem written with a greedy algorithm, written with linear time complexity and constant-level space complexity
  • We use isart and pstart to maintain the position where the asterisk appears
  • Traverse one side, start from the beginning, use i, j to indicate the current position of the two characters
  • If the character match is successful or the character in p is'? ', both move backward at the same time
  • If the above does not meet, then judge whether the character in p is'*'; if it is, assign i and j respectively to isart and pstart. (This is the second reason, I think: It is in line with the processing between the asterisk and the asterisk. If you encounter the following asterisk, replace it directly, because the role of the previous asterisk has been completed)
  • If the above two conditions are not met, we will i = istart + 1; j = pstart; This operation is the most critical and most in line with the meaning of the question. Because if the previous two conditions do not meet, we will use the asterisk. If it doesn't match, adjust i, and j to make the asterisk work.
  • If the above three are not in line, it also means that this does not meet the meaning of the question, directly return false
  • Finally, there is an operation to process the asterisk after p
  • The main purpose of this code is to understand its logic. The positions of these three judgments still need to be understood.

Source code :

class Solution {
    
    
public:
    bool isMatch(string s, string p) {
    
    
       int istart = -1, pstart = -1;
       int n_s = s.size();
       int n_p = p.size();

        int i = 0;
        int j = 0;
       while(i < n_s){
    
    
            if(j < n_p && s[i] ==p[j] || p[j] == '?'){
    
    
                i++;
                j++;
            }
            else if(j < n_p && p[j] == '*'){
    
    
                istart = i;
                pstart = j++;
            }
            else if(istart >= 0){
    
    
                i = ++istart;
                j = pstart + 1;
            }
            else{
    
    
                return false;
            }
        }

        while(j < n_p && p[j] == '*'){
    
    
            ++j;
        }  
        return j == n_p;
    }
};

Guess you like

Origin blog.csdn.net/qq_45914759/article/details/109315194