Dynamic planning:
1 classSolution {2 Public:3 /**4 * @param s:a string5 * @param p:a string includes "?" and "*"6 * @return: A boolean7 */8 BOOLIsMatch (Const Char*s,Const Char*p) {9 //Write your code hereTen intm = strlen (s), n =strlen (p); Onevector<BOOL> cur (m +1,false); Acur[0] =true; - for(intj =1; J <= N; J + +) { - BOOLPre = cur[0]; thecur[0] = cur[0] && P[j-1] =='*'; - for(inti =1; I <= m; i++) { - BOOLtemp =Cur[i]; - if(P[j-1] !='*') +Cur[i] = Pre && (s[i-1] = = P[j-1] || P[j-1] =='?'); - ElseCur[i] = cur[i-1] ||Cur[i]; +Pre =temp; A } at } - returnCur[m]; - } -};
Greedy:
1 classSolution {2 Public:3 /**4 * @param s:a string5 * @param p:a string includes "?" and "*"6 * @return: A boolean7 */8 BOOLIsMatch (Const Char*s,Const Char*p) {9 //Write your code hereTen Const Char* asterisk =NULL; One Const Char* match =NULL; A while(*s) { - if(*p = ='*') { -asterisk = p++; theMatch =s; - } - Else if(*s = = *p | | *p = ='?') { -s++; +p++; - } + Else if(asterisk) { As = + +match; atp = asterisk +1; - } - Else return false; - } - while(*p = ='*') p++; - return!*p; in } -};
[Lintcode] Wildcard query