题解 | #字符串通配符#

字符串通配符

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;
    }
}
全部评论

相关推荐

程序员花海:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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