zoukankan      html  css  js  c++  java
  • [leetcode]10. Regular Expression Matching(UNSOLVED)

    (leetcode给的范例已经不是模糊了 根本就是导向答题者往错误的地方想,还是要自己做好分类。

    错误的思路,无法处理字符串中央的.*代表匹配0次:

    class Solution:
        def isMatch(self, s: str, p: str) -> bool:
            # s p could be empty s(a-z) p(a-z . *)
            #* mean single letter ssss not mismismis
            lenS = len(s)
            lenP = len(p)
            # deal empty
            if lenP == 0:
                return False
            #.*
            if p =='.*':
                return True
            # deal p is pure alphabet
            if '.' not in p and '*' not in p:
                if p == s:
                    return True
                else:
                    return False
            # only '.' in p
            if '.' in p and '*' not in p:
                if lenS == lenP:
                    for index in range(lenS):
                        if p[index] == '.' or p[index] == s[index]:
                            if index == lenS - 1:
                                return True
                        else:
                            return False
                else:
                    return False
            # only '*' in p and # both '.' and '*' in p
            if lenS == 0:
                return True
    
            part = p.split("*")
            if part[len(part)-1] == '':
                part.pop()                    #-1 will remove '.' use pop
            else:
                if len(part[len(part)-1]) >1:
                    #have more than one mark in the end
                    lastone = part[len(part)-1]
                    for index in range(len(lastone)):
                        if lastone[index] == '.':
                            s = s[:-1]
                            part.pop()
                        else:
                            if part[len(part) - 1] == s[-1]:
                                s = s[:-1]
                                part.pop()
    
                else:
                    # have a single letter in the end or have a '.' in the end
                    if part[len(part) - 1] == '.':  # there have a '.' in the end
                        s = s[:-1]
                        part.pop()
                    else:
                        # have a single letter in the end
                        if part[len(part) - 1] == s[-1]:
                            s = s[:-1]
                            part.pop()
    
            for index in range(len(part)):   # -1 to remove '' in the end
                if part[index] == '.':
                    part[index] = s[0:1]    #first alphabet need to compare
                if len(part[index]) >1:
                    if part[index][0:len(part[index])-1] == s[0:len(part[index])-1]:
                        s = s[len(part[index]) - 1:]
                        part[index] = part[index][-1]
                    else:
                        return False
                while(True):
                    lenIndex =len(part[index])
                    if part[index] == s[0:lenIndex]:
                        s = s[lenIndex:]
                    else:
                        break
    
            if len(s) == 0:       #not lenS
                return True
            else:
                return False
  • 相关阅读:
    包含min函数的栈
    栈的应用
    给定金额m和红包数量n
    顺时针打印矩阵
    二叉树的镜像
    elementUI table表头错位问题
    金额格式化
    ajax跨域问题全解
    JavaScript 的 this 原理
    vue技术分享-你可能不知道的7个秘密
  • 原文地址:https://www.cnblogs.com/alfredsun/p/10809698.html
Copyright © 2011-2022 走看看