题解 | #字符串通配符#
字符串通配符
https://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036
import java.util.Scanner; //参考了官方的c++代码,写了个java的 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String a = in.nextLine(); String b = in.nextLine(); System.out.println(isMatchStr(a,b)); } in.close(); } private static boolean isMatchStr(String str1,String str2){ boolean[][] dp = new boolean[str1.length()+1][str2.length()+1]; for(int i=1;i<=str1.length();++i){ char ch1 = str1.charAt(i-1); dp[0][0] = true; dp[i][0] = dp[i-1][0] && (ch1=='*'); for(int j=1;j<=str2.length();++j){ char ch2 = str2.charAt(j-1); if(ch1=='*'){ dp[i][j] = dp[i][j-1] || dp[i-1][j]; }else{ if(Character.isLetter(ch2)){ dp[i][j] = dp[i-1][j-1] &&(ch1==ch2 || ch1=='?' || Character.toUpperCase(ch1)==Character.toUpperCase(ch2)); }else if(Character.isDigit(ch2)){ dp[i][j] = dp[i-1][j-1] &&(ch1==ch2 || ch1=='?'); }else{ dp[i][j] = dp[i-1][j-1] &&(ch1==ch2); } } } } return dp[str1.length()][str2.length()]; } }