C++ solution in string parameters


  • 5
    B
    class Solution {
    public:
        bool isMatch(string s, string p) {
            if(s.compare(p)) return true;
            int ssize = (int) s.size();
            int psize = (int) p.size();
            int i=0,j=0,pi=0,pj=psize;
            while(i<ssize){
                if(s[i]==p[j]||p[j]=='?'){
                    i++;
                    j++;
                    continue;
                }
                if(p[j]=='*'){
                    pj = j++;
                    pi = i;
                    continue;
                }
                if(pj<psize){
                    j = pj + 1;
                    i = ++pi;
                    continue;
                }
                return false;
            }
           while(p[j]=='*'){
               j++;
           }
           return j==psize;
        }
    };

  • 0
    M

    have you actually tried it? pop in (aa, a) and (aa, aa) and see what happens?


  • 0
    B

    thanks for your reply,I have edited it.Now ,it's correct!


  • 0
    R

    put explanations ..........................................


Log in to reply
 

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