**********************************递归************************************* public class Solution { public boolean match(char[] str, char[] pattern){ return judge(str,0,pattern,0); } public boolean judge(char[] str,int i, char[] pattern,int j){ int m=str.length; int n=pattern.length; if(i==m&&j==n)return true; if(i<m&&j==n) return false; if(i==m&&j<n){ if(j+1<n&&pattern[j+1]=='*'){ return judge(str,i,pattern,j+2); }else return false; } char a=str[i],b=pattern[j],c='a'; if(j+1<n){ c=pattern[j+1]; } if(c!='*'){ if(a==b||b=='.'){ return judge(str,i+1,pattern,j+1); } else return false; } else if(a==b||b=='.'){ return judge(str,i,pattern,j+2)||judge(str,i+1,pattern,j); }return judge(str,i,pattern,j+2); } } ****************************动态规划************************************** public class Solution { public boolean match(char[] str, char[] pattern) { int m=str.length; int n=pattern.length; if(m!=0&&n==0)return false;// boolean[][]dp=new boolean[m+1][n+1]; dp[0][0]=true;// for(int j=1;j<=n;j++){ if(pattern[j-1]=='*'&&dp[0][j-2]){ dp[0][j]=true; } } for(int i=1;i<=m;i++){ for(int j=1;j<=n;j++){ char a=str[i-1],b=pattern[j-1]; if(a==b||b=='.'){ dp[i][j]=dp[i-1][j-1]; } else if(b=='*'){ if(j>=2){ char c=pattern[j-2]; if(a==c||c=='.'){ dp[i][j]=dp[i][j-1]||dp[i-1][j]; }dp[i][j]=dp[i][j]||dp[i][j-2]; } }else dp[i][j]=false; } }return dp[m][n]; } }