C++ code (9ms) beats 100% persons!


  • 4
    L
    class Solution {
    public:
        bool isMatch(string s, string p) {
            int n=s.length(), m=p.length();
            int pid=0, sid=0;
            int pidp=-1, sidp=-1;
            while(sid<n){
                if(p[pid]==s[sid] || p[pid]=='?'){
                    pid++; sid++;
                }else if(p[pid]=='*'){
                    pidp=pid++; sidp=sid;
                }else{
                    if(pidp>-1){
                        pid=pidp+1; sid=++sidp;
                    }else{
                        return false;
                    }
                }
            }
            while(p[pid]=='*') pid++;
            return pid==m;
        }
    };
    

  • 2
    U

    very sharp!Make the number of characters a '*' represents as small as possible,some kind of greedy solution.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.