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

相关文章

macOS彻底卸载Python的超完整指南(推荐!)

《macOS彻底卸载Python的超完整指南(推荐!)》随着python解释器的不断更新升级和项目开发需要,有时候会需要升级或者降级系统中的python的版本,系统中留存的Pytho版本如果没有卸载干... 目录MACOS 彻底卸载 python 的完整指南重要警告卸载前检查卸载方法(按安装方式)1. 卸载

React 记忆缓存的三种方法实现

《React记忆缓存的三种方法实现》本文主要介绍了React记忆缓存的三种方法实现,包含React.memo、useMemo、useCallback,用于避免不必要的组件重渲染和计算,感兴趣的可以... 目录1. React.memo2. useMemo3. useCallback使用场景与注意事项在 Re

Python正则表达式匹配和替换的操作指南

《Python正则表达式匹配和替换的操作指南》正则表达式是处理文本的强大工具,Python通过re模块提供了完整的正则表达式功能,本文将通过代码示例详细介绍Python中的正则匹配和替换操作,需要的朋... 目录基础语法导入re模块基本元字符常用匹配方法1. re.match() - 从字符串开头匹配2.

SpringBoot3匹配Mybatis3的错误与解决方案

《SpringBoot3匹配Mybatis3的错误与解决方案》文章指出SpringBoot3与MyBatis3兼容性问题,因未更新MyBatis-Plus依赖至SpringBoot3专用坐标,导致类冲... 目录SpringBoot3匹配MyBATis3的错误与解决mybatis在SpringBoot3如果

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

Knife4j+Axios+Redis前后端分离架构下的 API 管理与会话方案(最新推荐)

《Knife4j+Axios+Redis前后端分离架构下的API管理与会话方案(最新推荐)》本文主要介绍了Swagger与Knife4j的配置要点、前后端对接方法以及分布式Session实现原理,... 目录一、Swagger 与 Knife4j 的深度理解及配置要点Knife4j 配置关键要点1.Spri

Qt QCustomPlot库简介(最新推荐)

《QtQCustomPlot库简介(最新推荐)》QCustomPlot是一款基于Qt的高性能C++绘图库,专为二维数据可视化设计,它具有轻量级、实时处理百万级数据和多图层支持等特点,适用于科学计算、... 目录核心特性概览核心组件解析1.绘图核心 (QCustomPlot类)2.数据容器 (QCPDataC

Java内存分配与JVM参数详解(推荐)

《Java内存分配与JVM参数详解(推荐)》本文详解JVM内存结构与参数调整,涵盖堆分代、元空间、GC选择及优化策略,帮助开发者提升性能、避免内存泄漏,本文给大家介绍Java内存分配与JVM参数详解,... 目录引言JVM内存结构JVM参数概述堆内存分配年轻代与老年代调整堆内存大小调整年轻代与老年代比例元空

深度解析Java DTO(最新推荐)

《深度解析JavaDTO(最新推荐)》DTO(DataTransferObject)是一种用于在不同层(如Controller层、Service层)之间传输数据的对象设计模式,其核心目的是封装数据,... 目录一、什么是DTO?DTO的核心特点:二、为什么需要DTO?(对比Entity)三、实际应用场景解析

Go语言中nil判断的注意事项(最新推荐)

《Go语言中nil判断的注意事项(最新推荐)》本文给大家介绍Go语言中nil判断的注意事项,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.接口变量的特殊行为2.nil的合法类型3.nil值的实用行为4.自定义类型与nil5.反射判断nil6.函数返回的