class Solution {
public boolean isMatch(String s, String p) {
int sLen = s.length(), pLen = p.length();
int sIdx = 0, pIdx = 0;
int starIdx = -1, sTmpIdx = -1;
while (sIdx < sLen) {
if (pIdx < pLen && (p.charAt(pIdx) == '?' || p.charAt(pIdx) == s.charAt(sIdx))){
++sIdx;
++pIdx;
} else if (pIdx < pLen && p.charAt(pIdx) == '*') {
starIdx = pIdx;
sTmpIdx = sIdx;
++pIdx;
} else if (starIdx == -1) {
return false;
} else {
pIdx = starIdx + 1;
sIdx = sTmpIdx + 1;
sTmpIdx = sIdx;
}
}
for(int i = pIdx; i < pLen; i++)
if (p.charAt(i) != '*') return false;
return true;
}
}
Runtime: 2 ms, faster than 100.00% of Java online submissions for Wildcard Matching.
Memory Usage: 38.6 MB, less than 99.84% of Java online submissions for Wildcard Matching.