首页 > 其他 > 详细

[leetcode]10. Regular Expression Matching

时间:2019-05-04 20:51:42      阅读:152      评论:0      收藏:0      [点我收藏+]

(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

 

[leetcode]10. Regular Expression Matching

原文:https://www.cnblogs.com/alfredsun/p/10809698.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!