题解 | #字符串通配符#
字符串通配符
http://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036
递归思想 ,新手一枚,还是转动态规划好!!!狗头保命
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String regrex = in.next();
String str = in.next();
str = str.toLowerCase();
regrex = regrex.toLowerCase();
for(int j=0;j<regrex.length()-1;){
if(regrex.substring(j,j+1).equals("*"))
{
if(regrex.substring(j+1,j+2).equals("*")){
regrex = regrex.substring(0,j)+regrex.substring(j+1);
}else{
j++;
}
}else{
j++;
}
}
System.out.println(getmatches(str,regrex,0,0));
}
public static boolean getmatches(String str,String regrex,int i,int j){
if(i==str.length()&&j==regrex.length())
return true;
while(i<str.length()&&j<regrex.length()){
String reg = regrex.substring(j,j+1);
char a = str.charAt(i);
if(str.substring(i,i+1).equals(reg)){
i++;j++;
}else if(reg.equals("?")&&((a>='a'&&a<='z')||(a>='0'&&a<='9'))){
i++;
j++;
}else if(reg.equals("*")){
return getmatches(str,regrex,i,j+1)||getmatches(str,regrex,i+1,j+1)||getmatches(str,regrex,i+1,j);
}else{
break;
}
if(i==str.length()&&j==regrex.length())
return true;
}
return false;
}
}
