题解 | #字符串通配符#

字符串通配符

https://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while (sc.hasNext()){
            String str1=sc.nextLine();
            String str2=sc.nextLine();
            boolean flag=math(str1,str2);
            System.out.println(flag);
        }
    }
    public static boolean math(String t,String s){
        char[] ct= t.toCharArray();
        char[] cs=s.toCharArray();
        int lt=ct.length;
        int ls=cs.length;
        boolean[][] dp=new boolean[ls+1][lt+1];
        dp[0][0]=true;
        for (int i = 0; i <= ls; i++) {
            for (int j = 1; j <= lt; j++) {
                if(ct[j-1]=='*'){
                    if(i==0){
                        dp[i][j]=dp[i][j-1];
                    }else {
                        if(cs[i-1]=='.'||(cs[i-1]>='0'&&cs[i-1]<='9')||(cs[i-1]>='a'&&cs[i-1]<='z')||(cs[i-1]>='A'&&cs[i-1]<='Z')){
                            dp[i][j]=dp[i-1][j]||dp[i][j-1];
                        }
                    }
                }else {
                    if(i>0&&defs(ct[j-1],cs[i-1])){
                        dp[i][j]=dp[i-1][j-1];
                    }
                }
            }
        }
        return dp[ls][lt];
    }
    public static boolean defs(char t,char s){
        if(t=='?'){
            return true;
        }
        if(t>='a'&&t<='z'){
            t=(char)(t-'a'+'A');
        }
        if(s>='a'&&s<='z'){
            s=(char)(s-'a'+'A');
        }
        return s==t;
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
迟缓的斜杠青年巴比Q...:简历被投过的公司卖出去了,我前两天遇到过更离谱的,打电话来问我有没有意向报班学Java学习,服了,还拿我学校一个学长在他们那报班学了之后干了华为OD当招牌
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务