LeetCode(10)Regular Expression Matching

题目如下:

Python代码:

# -*- coding:utf-8 -*-
def ismatch(s,p):
    #先将dp[s+1][p+1]二维数组全置为False
    dp = [[False] * (len(s) + 1) for _ in range(len(p)+1)]
    dp[0][0] = True
    for i in range(1,len(p)):
        dp[i+1][0] = dp[i-1][0] and p[i] == ‘*‘
    for i in range(len(p)):
        for j in range(len(s)):
            if p[i]==‘*‘:
                # or运算相当于并,and相当于交
                dp[i+1][j+1] = dp[i-1][j+1] or dp[i][j+1]
                if p[i-1] == s[j] or p[i-1] == ‘.‘:
                    # |=相当于并,&=相当于交
                    dp[i+1][j+1] |= dp[i+1][j]
            else:
                dp[i+1][j+1] = dp[i][j] and (p[i] == s[j] or p[i] == ‘.‘)
    return dp[-1][-1]

print ismatch(‘aab‘,‘c*a*b*‘)
时间: 2024-10-07 05:05:51

LeetCode(10)Regular Expression Matching的相关文章

LeetCode 10: Regular Expression Matching

Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be

leetcode第十题--Regular Expression Matching

Problem:Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype s

LeetCode(10) - Regular Expression Matching

非常challenge的一道题,考虑的情况特别多,当考虑到了之后,还要考虑怎么处理.题目要求是,给你两个字符串,判断正则字符串能不能表示待检字符串,一个是"aabb",另外一个是正则表达式"a*.*".该正则表达式只有两种特殊符号,一种是'.',代表的是任意字符,另一种是'*',它单独出现没有任何意义,要搭配前一个如"a*",则说明a可以连续出现零到多次,根据这个定义,上面给的例子返回的就是true.总体来说,它要注意的是以下几种情况(s是待检字

Leetcode(10)-实现strStr()

实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返回  -1. 当 needle 是空字符串时我们应当返回 0 . 一开始的思路:用i和j从头开始分别遍历haystack和needle字符串,先固定j,直到i所指的字母与needle的首字符相等的时候,停止遍历.然后i和j开始同时向后遍历,如果有不相等的,就返回-1,直到有一个字符串先到头.若hay

LeetCode 10 Regular Expression Matching (正则表达式匹配)

翻译 实现支持"."和"*"的正则表达式匹配. "." 匹配支持单个字符 "*" 匹配零个或多个前面的元素 匹配应该覆盖到整个输入的字符串(而不是局部的). 该函数的原型应该是: bool isMatch(const char * s, const char * p) 示例: isMatch("aa","a") → false isMatch("aa","a

Leetcode 10 regular expression matching (正则表达式匹配) (动态规划)

Leetcode 10 问题描述 Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entir

【leetcode】Regular Expression Matching (hard) ★

Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be

LeetCode (10): Regular Expression Matching [HARD]

https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the ent

leetcode problem 10 Regular Expression Matching

Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be