Wildcard Matching

来源:互联网 发布:linux 给程序root权限 编辑:程序博客网 时间:2024/06/06 03:04

11/1

这个难题一遍做出来了

public class Solution {    public boolean isMatch(String s, String p) {        int slen = s.length(), plen = p.length();        int ss = 0, pp = 0;                int sc = -1, pc = -1;        while(ss < slen){            if(pp < plen && p.charAt(pp) == '*'){                sc = ss;                pc = pp;                pp++;            }else if(pp >= plen || (pp < plen && p.charAt(pp) != '?' && s.charAt(ss) != p.charAt(pp))){                if(sc < 0){ // nothing cached before                    return false;                }else{  // restore to where previously cached                    ss = ++sc;                    pp = pc;                }            }else{                ss++;                pp++;            }        }        // note that it is okay if pp doesn't hit the end; just check if        // only *'s remain        for(; pp < plen; pp++){            char pchar = p.charAt(pp);            if(pchar != '*')    return false;        }                return true;    }}


0 0