10. Regular Expression Matching

2023-05-16

10. Regular Expression Matching

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 entire input string (not partial).

Note:

  • s could be empty and contains only lowercase letters a-z.
  • p could be empty and contains only lowercase letters a-z, and characters like . or *.

Example 1:

Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".

Example 2:

Input:
s = "aa"
p = "a*"
Output: true
Explanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".

Example 3:

Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".

Example 4:

Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches "aab".

Example 5:

Input:
s = "mississippi"
p = "mis*is*p*."
Output: false

solution

在这里插入图片描述

class Solution {
   public boolean isMatch(String s, String p) {
        if(s == null  || p == null) return false;

        boolean[][] match = new boolean[s.length() + 1][p.length() + 1];

        match[0][0] = true;

        for(int j = 1; j <= p.length(); j++){

            if(p.charAt(j - 1) == '*' && match[0][j-2]){
                match[0][j] = true;
            }
        }


        for(int i = 1; i <= s.length(); i++){
            for(int j = 1; j <= p.length(); j++){
                if(s.charAt(i-1) == p.charAt(j-1) || p.charAt(j-1) == '.' ){
                    match[i][j] = match[i-1][j-1];
                }else if(p.charAt(j-1) == '*'){
                    if(s.charAt(i-1) == p.charAt(j-2) || p.charAt(j-2) == '.'){
                        match[i][j] = match[i-1][j] || match[i][j-2] || match[i][j-1];
                    }else{
                        match[i][j] = match[i][j-2];
                    }
                }
            }
        }

        return match[s.length()][p.length()];

    }
}

solution2

参考我的另一篇博客:正则表达式匹配

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

10. Regular Expression Matching 的相关文章

  • 如何在 C# 中运行时计算字符串格式的布尔表达式的结果?

    假设我从文件中读取了这个条件 Condition Person Value Status 9 如果 Person 是我的代码中的一个类 如何在运行时检查此条件是否成立 虽然我自己没有亲自这样做过 this http www codeproj
  • PostgreSQL 中表达式索引的实际限制

    我需要使用 HSTORE 类型和键索引来存储数据 CREATE INDEX ix product size ON product data gt Size INT CREATE INDEX ix product color ON produ
  • 我想在列之间匹配相似的单词

    1 0 2 0 3 0 loud complaint problems pain stress confused dull pain stress 这是我的数据集 我想重新组织行 以便如果每列中出现一个单词 它就会转移到相应的行 例如 1
  • Python-评估字符串中的数学表达式[重复]

    这个问题在这里已经有答案了 我有一个关于字符串内数学表达式求值的问题 例如我的字符串如下 my str I have 6 2 3 apples 我想知道如何评估这个字符串并得到以下结果 I have 30 apples 有什么办法可以做到这
  • 覆盖 grails.views.default.codec='html' 配置回 'none'

    在 Grails grails views default code none 在 grails Config groovy 中 我可以在 GSP 文件中显式对表达式进行 HTML 编码 myValue encodeAsHTML 如果我设置
  • PHP Preg表达式从字符串中删除html标签和内部内容?

    快速提问 我想从以下字符串中删除sup 标签及其中的所有内容 string Priority Mail
  • 匹配大文本文件中的字符串?

    我有一个字符串列表 其中包含大约 700 万个项目 大小为 152MB 的文本文件 我想知道实现 a 函数的最佳方法是什么 该函数接受单个字符串并返回它是否在该字符串列表中 您是否需要多次匹配此文本文件 如果是这样 我会创建一个HashSe
  • 使用正则表达式匹配数字 - 仅数字和逗号

    我无法弄清楚如何为示例值构建正则表达式 123 456 789 12 34 1234 8 你可以帮帮我吗 什么是数字 我有一个简单的问题your 简单 问题 一个数字 到底是什么意思 Is 0一个号码 你对这个怎么看 1 Is or 一个号
  • 逻辑表达式解析器

    我正在尝试为以下表达式创建一个逻辑表达式解析器 变量A gt 变量B 而不是变量C 对于给定的变量值 解析器应该能够返回结果是 true 还是 false 基本上 表达式仅包含变量 逻辑运算符 或 与 蕴涵 等价 否定和括号 我想问实现这种
  • XPath 1.0 用于查找元素的值是否在值列表中

    有没有办法构造一个 XPath 来评估元素的值是否在预定义的值列表中 与此类似的东西 Location Addr State TX or AL or MA 哪一个将与德克萨斯州 阿拉巴马州或马萨诸塞州的州元素相匹配 我知道我无法解压该表达式
  • x&&y||z 是如何计算的?

    Given int x 1 y 2 z 您能解释一下为什么结果是 x y z is 1 x y 1 x y z 1 x y z 相当于 x y z if x 1 and y 2 then x y is 1 2这是true true这是tru
  • 与有向边的最大加权二分匹配

    我知道计算最大加权匹配的各种算法加权 无向二分图 即分配问题 例如 匈牙利算法 贝尔曼 福特算法甚至 Blossom 算法 适用于一般图 即非二分图 但是 如果二分图的边是 如何计算最大加权匹配加权和定向 我希望能够提供具有多项式复杂度的算
  • 为什么语句“m = ++i ||”中的“k”不递增++j && ++k”? [复制]

    这个问题在这里已经有答案了 第 1 部分 i j k 1 m i j k printf d d d d n i j k m 输出 2 2 1 1 第一部分很容易理解 在这里 i j首先执行 这是正确的 并且增加 i 和 j 的值 因此不需要
  • C# - 无法在 lambda 表达式中使用“is”运算符

    我正在使用 AgileMapper 和以下代码 source Map OnTo target options gt options IgnoreSources options gt options If value gt value is
  • 如何将 PHP 标签与正则表达式匹配? [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我不太擅长正则表达式 你能给我一个匹配任何 php 标签的模式吗 is php blocks print r blocks 一个问题是 如
  • 重命名命令中的下划线(Perl 和 Unix shell)

    我正在尝试替换所有 下划线字符为 所有文件名中的连字符 mat在一个文件夹内 我输入不同版本但未成功 rename f w mat 有人可以向我解释一下出了什么问题吗 如果您正在使用基于 Perl 的rename http socialte
  • 在python中查找关键字后的单词[重复]

    这个问题在这里已经有答案了 我想查找出现在关键字 由我指定和搜索 之后的单词并打印出结果 我知道我应该使用正则表达式来做到这一点 我也尝试了一下 如下所示 import re s hi my name is ryan and i am ne
  • 反射性能 - 创建委托(C# 属性)

    我在使用反射时遇到性能问题 所以我决定为我的对象的属性创建委托 到目前为止得到了 TestClass cwp new TestClass var propertyInt typeof TestClass GetProperties Sing
  • WhereNot linq 表达式

    我正在尝试创建一个扩展 WhereNot 所以我可以使用 Dim x Hello world Dim y x Split WhereNot AddressOf String IsNullOrEmpty 请注意 我的目标是学习 linq 表达
  • 在 R 中匹配多个日期值

    我有以下数据框 DF 描述在特定日期从事项目的人员 ID ProjectName StartDate 1 Health 3 1 06 18 20 2 Education 2 1 07 15 30 1 Education 5 3 09 9 0

随机推荐

  • Ubuntu 18.04和windows建立共享文件夹

    1 安装samba sudo apt install samba sudo apt install cifs utils 2 创建共享目录 mkdir home yourname share yourname是home下一个文件夹 xff0
  • Linux权限详解,多用户多组各种权限配置原理

    网上有太多关于Linux权限详解 xff0c 这里不啰嗦那些 主要解释下这些权限是杂用的 xff0c 否则知道了什么用户 组之类的权限也配不好 一 权限分类 r xff1a 读取权限 xff0c 数字代号为 34 4 34 w xff1a
  • 1.tow sum

    文章目录 题目c 43 43 版本java版本利用hashmap正确做法 题目 Two Sum Easy Given an array of integers return indices of the two numbers such t
  • 2. Add Two Numbers

    2 Add Two Numbers Medium 59971568Share You are given two non empty linked lists representing two non negative integers T
  • Linux VNC server的安装及简单配置使用

    Linux VNC server的安装及简单配置使用 转 xff1a http www 2cto com os 201309 241104 html Linux VNC server的安装及简单配置使用 摘要 xff1a Linux vnc
  • 3. Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters Given a string find the length of the longest substring without repeating
  • 4. Median of Two Sorted Arrays

    Median of Two Sorted Arrays Hard There are two sorted arrays nums1 and nums2 of size m and n respectively Find the media
  • 7. Reverse Integer

    文章目录 Reverse IntegersolutionAlgorithm Reverse Integer Easy Given a 32 bit signed integer reverse digits of an integer Ex
  • 8. String to Integer (atoi)

    String to Integer atoi Implement atoi which converts a string to an integer The function first discards as many whitespa
  • 9. Palindrome Number

    Palindrome Number Easy Determine whether an integer is a palindrome An integer is a palindrome when it reads the same ba
  • 1071. Greatest Common Divisor of Strings

    1071 Greatest Common Divisor of Strings Easy 30985Share For strings S and T we say 34 T divides S 34 if and only if S 61
  • 这个问题搞了我一天

  • 150逆波兰式

    文章目录 150 Evaluate Reverse Polish NotationSolution 150 Evaluate Reverse Polish Notation Medium Evaluate the value of an a
  • 收到礼物最大值

    题目描述 在一个m n的棋盘的每一个格都放有一个礼物 xff0c 每个礼物都有一定价值 xff08 大于0 xff09 从左上角开始拿礼物 xff0c 每次向右或向下移动一格 xff0c 直到右下角结束 给定一个棋盘 xff0c 求拿到礼物
  • 64. Minimum Path Sum

    64 Minimum Path Sum Given a m x n grid filled with non negative numbers find a path from top left to bottom right which
  • 找出亲密对数

    题目内容 xff1a 求数n之内的亲密对数 所谓 亲密对数 xff0c 即A的所有因子 xff08 包含1但不包含其本身 xff09 之和等于B xff0c 而B的所有因子之和等于A 输入格式 某个数字n 输出格式 xff1a 此数字n之内
  • 5. Longest Palindromic Substring

    5 Longest Palindromic Substring Given a string s find the longest palindromic substring in s You may assume that the max
  • 516. Longest Palindromic Subsequence

    516 Longest Palindromic Subsequence Given a string s find the longest palindromic subsequence s length in s You may assu
  • 第一讲_网站架构的演变及海量数据的解决方案

    文章目录 看透springMVC 读书笔记 第一讲单机类型CS结构 xff08 Client Server xff09 BS结构 xff08 Browser Server xff09 BS结构网络传输方式OSI七层模型 TCP IP四层模型
  • 10. Regular Expression Matching

    10 Regular Expression Matching Given an input string s and a pattern p implement regular expression matching with suppor