public boolean match(char[] str, char[] pattern) { return String.valueOf(str).matches(String.valueOf(pattern)); }
public class Solution { public boolean match(char[] str, char[] pattern) { if(str ==null||pattern==null){ return false; } return help(str,0,pattern ,0); } public boolean help(char[] str,int sindex,char[] pattern,int pindex){ if(sindex == str.length&& pindex==pattern.length){ return true; } if(pindex==pattern.length){ return false; } if((pindex+1<pattern.length)&&pattern[pindex+1]=='*'){ if(sindex!=str.length&&(pattern[pindex]=='.'||pattern[pindex]==str[sindex])){//判斷sindex防止數組溢出 return help(str,sindex,pattern,pindex+2)||help(str,sindex+1,pattern,pindex+2)||help(str,sindex+1,pattern,pindex);//匹配0個字符,一個字符,一直匹配 } else{ return help(str,sindex,pattern,pindex+2); } } if(sindex!=str.length&&(pattern[pindex]=='.'||pattern[pindex]==str[sindex])){ return help(str,sindex+1,pattern,pindex+1); } return false; } }