题解 | #字符串通配符#

字符串通配符

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

def regexp(rule, s):
    rule = rule.upper()
    s = s.upper()
    start = rule[0]
    ss = start
    for i in range(1, len(rule)):
        if (start == "*" and rule[i] == "*"):
            continue
        else:
            ss += rule[i]
        start = rule[i]
    rule=ss
    rule1 = rule
    s1 = s
    for i in range(len(rule)):
        if (len(s) - 1) >= i:
            if rule[i] == s[i] or (rule[i] == "?" and (ord("Z") >= ord(s[i]) >= ord("A") or s[i] == "?" or "9">=s[i]>="0")):
                rule1 = rule[(i + 1):]
                s1 = s[(i + 1):]
            elif(rule[i]=="*"):
                break
            else:
                return False
        else:
            for i in range(len(rule1)):
                if ord("Z") >= ord(rule1[i]) >= ord("A") or rule1[i] == "?" or "9">=rule1[i]>="0":
                    return False
            return True
    rule=rule1
    s=s1
    for i in range(-1, -1 * (len(rule) + 1), -1):
        if len(s) * -1 <= i:
            if rule[i] == s[i] or (rule[i] == "?" and (ord("Z") >= ord(s[i]) >= ord("A") or s[i] == "?" or "9">=s[i]>="0")):
                rule1 = rule[:i]
                s1 = s[:i]
            elif (rule[i] == "*"):
                break
            else:
                return False
        else:
            for i in range(len(rule1)):
                if ord("Z") >= ord(rule1[i]) >= ord("A") or rule1[i] == "?":
                    return False
            return True
    if len(rule1)==0:
        if len(s1)==0:
            return True
        else:
            return False
    if rule1.strip("*") !="":
        rule1= rule1.strip("*")
    else:
        for x in s1:
            if("0">=x>="9" or "Z">=x>="A"):
                continue
            else:
                return False
        return True
    for i in range(len(s1)):
        for j in range(i, len(s1)):
            flag = regexp(rule1, s1[i:j+1])
            if (flag):
                return True
    return False


rule = input()
s = input()
print("true") if regexp(rule, s) else print("false")


全部评论

相关推荐

11-06 10:58
已编辑
门头沟学院 嵌入式工程师
双非25想找富婆不想打工:哦,这该死的伦敦腔,我敢打赌,你简直是个天才,如果我有offer的话,我一定用offer狠狠的打在你的脸上
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
11-26 15:46
已编辑
字节国际 电商后端 24k-35k
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务