LeetCode 44 Wildcard Matching (通配符匹配 记忆化搜索 剪枝 推荐)

本文主要是介绍LeetCode 44 Wildcard Matching (通配符匹配 记忆化搜索 剪枝 推荐),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'.

'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).

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 = "*"
Output: true
Explanation: '*' matches any sequence.

Example 3:

Input:
s = "cb"
p = "?a"
Output: false
Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'.

Example 4:

Input:
s = "adceb"
p = "*a*b"
Output: true
Explanation: The first '*' matches the empty sequence, while the second '*' matches the substring "dce".

Example 5:

Input:
s = "acdcb"
p = "a*c?b"
Output: false

题目链接:https://leetcode.com/problems/wildcard-matching/

题目分析:与正则匹配比较类似,本题不用记忆化会直接超时,多个连续的'*'可直接缩成1个,功能不会产生变化

25ms,时间击败76.9% (运行时间不是很稳定,21ms~28ms)

class Solution {public boolean isFirstPosMatch(String s, int spos, String p, int ppos) {return (spos < s.length() &&(s.charAt(spos) == p.charAt(ppos) ||p.charAt(ppos) == '?'));}public boolean isMatch(String s, String p) {int[][] match = new int[s.length() + 2][p.length() + 2];return isMatchHelper(s, 0, p, 0, match);}public boolean isMatchHelper(String s, int spos, String p, int ppos, int[][] match) {if (match[spos][ppos] != 0) {return match[spos][ppos] == 1;}if (ppos == p.length()) {match[spos][ppos] = spos == s.length() ? 1 : -1;return match[spos][ppos] == 1;}if (p.charAt(ppos) != '*') {match[spos][ppos] = isFirstPosMatch(s, spos, p, ppos) && isMatchHelper(s, spos + 1, p, ppos + 1, match) ? 1 : -1;return match[spos][ppos] == 1;}while (ppos < p.length() && p.charAt(ppos) == '*') {ppos++;}while (spos < s.length()) {match[spos][ppos] = isMatchHelper(s, spos, p, ppos, match) ? 1 : -1;if (match[spos][ppos] == 1) {return true;}spos++;}match[spos][ppos] = isMatchHelper(s, spos, p, ppos, match) ? 1 : -1;return match[spos][ppos] == 1;}
}

最后的while循环看上去是比较耗时的,可以从中挖掘一些剪枝,通配符匹配和正则匹配的一大不同就是通配符匹配中模式串的确定字符必须要在主串上严格匹配(正则可通过'*'消去前一项),于是想到用两个cnt数组分别记录s和p中字符串的个数,设当前匹配到了s的第i项s[i],若pcnt[s[i]] > scnt[s[i]],则说明s串接下来的s[i]字符已不够p串匹配了,故此时的'*'不能消掉s[i]

加了这个剪枝后,6ms,时间击败97.9%

class Solution {public boolean isFirstPosMatch(String s, int spos, String p, int ppos) {return (spos < s.length() &&(s.charAt(spos) == p.charAt(ppos) ||p.charAt(ppos) == '?'));}public boolean isMatch(String s, String p) {int[] scnt = new int[26];int[] pcnt = new int[26];for (int i = 0; i < s.length(); i++) {scnt[s.charAt(i) - 'a']++;}for (int i = 0; i < p.length(); i++) {char ch = p.charAt(i);if (ch >= 'a' && ch <= 'z') {pcnt[ch - 'a']++;}}int[][] match = new int[s.length() + 2][p.length() + 2];return isMatchHelper(s, 0, p, 0, match, scnt, pcnt);}public boolean isMatchHelper(String s, int spos, String p, int ppos, int[][] match, int[] scnt, int[] pcnt) {if (match[spos][ppos] != 0) {return match[spos][ppos] == 1;}if (ppos == p.length()) {match[spos][ppos] = spos == s.length() ? 1 : -1;return match[spos][ppos] == 1;}if (p.charAt(ppos) != '*') {boolean ok = isFirstPosMatch(s, spos, p, ppos);if (ok) {scnt[s.charAt(spos) - 'a']--;if (p.charAt(ppos) != '?') {pcnt[p.charAt(ppos) - 'a']--;}ok &= isMatchHelper(s, spos + 1, p, ppos + 1, match, scnt, pcnt);if (p.charAt(ppos) != '?') {pcnt[p.charAt(ppos) - 'a']++;}scnt[s.charAt(spos) - 'a']++;}match[spos][ppos] = ok ? 1 : -1;return ok;}while (ppos < p.length() && p.charAt(ppos) == '*') {ppos++;}while (spos < s.length() && pcnt[s.charAt(spos) - 'a'] <= scnt[s.charAt(spos) - 'a']) {match[spos][ppos] = isMatchHelper(s, spos, p, ppos, match, scnt, pcnt) ? 1 : -1;if (match[spos][ppos] == 1) {return true;}scnt[s.charAt(spos) - 'a']--;   spos++;}match[spos][ppos] = isMatchHelper(s, spos, p, ppos, match, scnt, pcnt) ? 1 : -1;return match[spos][ppos] == 1;}
}

 

这篇关于LeetCode 44 Wildcard Matching (通配符匹配 记忆化搜索 剪枝 推荐)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/829469

相关文章

MySQL 存储引擎 MyISAM详解(最新推荐)

《MySQL存储引擎MyISAM详解(最新推荐)》使用MyISAM存储引擎的表占用空间很小,但是由于使用表级锁定,所以限制了读/写操作的性能,通常用于中小型的Web应用和数据仓库配置中的只读或主要... 目录mysql 5.5 之前默认的存储引擎️‍一、MyISAM 存储引擎的特性️‍二、MyISAM 的主

C++ HTTP框架推荐(特点及优势)

《C++HTTP框架推荐(特点及优势)》:本文主要介绍C++HTTP框架推荐的相关资料,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Crow2. Drogon3. Pistache4. cpp-httplib5. Beast (Boos

Python多进程、多线程、协程典型示例解析(最新推荐)

《Python多进程、多线程、协程典型示例解析(最新推荐)》:本文主要介绍Python多进程、多线程、协程典型示例解析(最新推荐),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定... 目录一、multiprocessing(多进程)1. 模块简介2. 案例详解:并行计算平方和3. 实现逻

Nginx路由匹配规则及优先级详解

《Nginx路由匹配规则及优先级详解》Nginx作为一个高性能的Web服务器和反向代理服务器,广泛用于负载均衡、请求转发等场景,在配置Nginx时,路由匹配规则是非常重要的概念,本文将详细介绍Ngin... 目录引言一、 Nginx的路由匹配规则概述二、 Nginx的路由匹配规则类型2.1 精确匹配(=)2

Spring Boot集成SLF4j从基础到高级实践(最新推荐)

《SpringBoot集成SLF4j从基础到高级实践(最新推荐)》SLF4j(SimpleLoggingFacadeforJava)是一个日志门面(Facade),不是具体的日志实现,这篇文章主要介... 目录一、日志框架概述与SLF4j简介1.1 为什么需要日志框架1.2 主流日志框架对比1.3 SLF4

Springboot实现推荐系统的协同过滤算法

《Springboot实现推荐系统的协同过滤算法》协同过滤算法是一种在推荐系统中广泛使用的算法,用于预测用户对物品(如商品、电影、音乐等)的偏好,从而实现个性化推荐,下面给大家介绍Springboot... 目录前言基本原理 算法分类 计算方法应用场景 代码实现 前言协同过滤算法(Collaborativ

Maven中引入 springboot 相关依赖的方式(最新推荐)

《Maven中引入springboot相关依赖的方式(最新推荐)》:本文主要介绍Maven中引入springboot相关依赖的方式(最新推荐),本文给大家介绍的非常详细,对大家的学习或工作具有... 目录Maven中引入 springboot 相关依赖的方式1. 不使用版本管理(不推荐)2、使用版本管理(推

Nginx location匹配模式与规则详解

《Nginxlocation匹配模式与规则详解》:本文主要介绍Nginxlocation匹配模式与规则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、环境二、匹配模式1. 精准模式2. 前缀模式(不继续匹配正则)3. 前缀模式(继续匹配正则)4. 正则模式(大

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思

Python中使用正则表达式精准匹配IP地址的案例

《Python中使用正则表达式精准匹配IP地址的案例》Python的正则表达式(re模块)是完成这个任务的利器,但你知道怎么写才能准确匹配各种合法的IP地址吗,今天我们就来详细探讨这个问题,感兴趣的朋... 目录为什么需要IP正则表达式?IP地址的基本结构基础正则表达式写法精确匹配0-255的数字验证IP地