1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public class Solution { public boolean match(char[] str, char[] pattern) { if( str==null || pattern==null ) return false; return matchSub(str, 0, pattern, 0); } public boolean matchSub(char[] str, int strIdx, char[] p, int pIdx){ if( strIdx==str.length && pIdx==p.length ) return true; if( strIdx != str.length && pIdx==p.length ) return false; if( pIdx+1 < p.length && p[pIdx+1]=='*' ){ if( strIdx!=str.length && str[strIdx]==p[pIdx] || strIdx != str.length && p[pIdx]=='.' ){ return matchSub(str, strIdx, p, pIdx+2) || matchSub(str, strIdx+1, p, pIdx) || matchSub(str, strIdx+1, p, pIdx+2); }else { return matchSub(str, strIdx, p, pIdx+2); } }
if( strIdx != str.length && (str[strIdx] == p[pIdx] || p[pIdx]=='.') ){ return matchSub(str, strIdx+1, p, pIdx+1); } return false; } }
|