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
  • 相关阅读:
    重新理解js的执行环境和闭包
    给开发插上想象力的翅膀
    Vue源码的初始化以及数据驱动逻辑
    解析Vue源码之前
    前端模块化发展介绍和未来展望
    现代前端框架具备的特征分析及Vue、React对比
    始于Flux的单项数据流发展简单介绍
    用面向对象编程解决常见需求场景
    【Docker】之重启容器相关命令
    【Java】之获取CSV文件数据以及获取Excel文件数据
  • 原文地址:https://www.cnblogs.com/alfredsun/p/10809698.html
Copyright © 2011-2022 走看看