当前位置:网站首页>Brush LeetCode topic series - 10. Regular expression match
Brush LeetCode topic series - 10. Regular expression match
2022-08-02 06:12:00 【Wooden water in the river island】
Given a string s and a character pattern p, please implement a regular expression matching that supports '.' and '*'.
'.' matches any single character
'*' matches zero or more of the preceding elements
The so-called match is to cover the entire string s, not part of the string.
Example 1:
Input: s = "aa", p = "a"
Output: false
Explanation: "a" cannot match the entire string "aa".
Example 2:
Input: s = "aa", p = "a*"
Output: true
Explanation: Because '*' means it can match zero or more of the preceding elements, here the preceding elementThe element is just 'a'.Therefore, the string "aa" can be treated as 'a' repeated once.
Example 3:
Input: s = "ab", p = ".*"
Output: true
Explanation: ".*" matches zero or more ('*') arbitrary characters ('.').
Source: LeetCode
Link: https://leetcode.cn/problems/regular-expression-matching
Ideas: Big Guy: Classic Dynamic Programming: Regular Expressions :: labuladong's algorithm cheat sheet
Thinking: This question is still a bit difficult.1. Define the function dp((string)s, (string)p, (int)i, (int)j), when dp(s, p, i, j) == true,Indicates that s[i,...] and p[j,...] are regular matches2. For any i and j, s[i] and p[j] have two states, either matching or not matchingThe pseudo code is as follows:if s[i] == p[j] or p[j] = '.' { // when s[i] matches p[j]if j < p.size() - 1 and p[j+1] == '*' {// For * , there are two cases,// 1. p[j] matches the character in s 0 times; 2. p[j] matches the character in s multiple timesbool is_metch_zero = dp(s, p, i, j+2); // p[j] matches the character in s 0 timesbool is_match_one_or_more = dp(s, p, i+1, j); // p[j] matches the character in s multiple timesreturn is_metch_zero || is_match_one_or_more;} else {// When not *, does not involve matching 0 or more timesreturn dp(s, p, i+1, j+1);}} else { // when s[i] does not match p[j]if j < p.size() - 1 and p[j+1] == '*' {return dp(s, p, i, j+2); // match 0 times, continue to look down} else {return false;}}Of course, iteration specifies the case that cannot pass the question. The reason is very simple. Iteration will calculate repeated cases.at this time,We can use a two-dimensional array memp to record the results of midway calculations to avoid repeated case calculations.memp[i][j] indicates whether s[i,...] and p[j,...] are regular matchesThe java code is as follows:
class Solution {public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {// Define map, key is a node in the graph, value is (in-degree node, weight value)// to -> (from, price)Map> map = new HashMap<>();for (int[] dists : flights) {int from = dists[0];int to = dists[1];int price = dists[2];int[] subValue = {from, price};if (!map.containsKey(to)) {List inValues = new LinkedList<>();inValues.add(subValue);map.put(to, inValues);} else {map.get(to).add(subValue);}}// record table, record the minimum cost from src to memp[i][j] and the number of transfers is jint[][] memp = new int[n][k + 1];for (int[] value : memp) {Arrays.fill(value, -888);}return dp(src, dst, k+1, map, memp);}int dp(int src, int dst, int k, Map> map, int[][] memp) {if (dst == src) {return 0;}if (k == 0) {return -1;}int res = Integer.MAX_VALUE;// when dst has in-degree nodesif (map.containsKey(dst)) {for (int[] subValues : map.get(dst)) {int from = subValues[0];int price = subValues[1];int fromValue = memp[from][k - 1];if (fromValue == -888) {fromValue = dp(src, from, k - 1, map, memp);memp[from][k - 1] = fromValue;}if (fromValue != -1) {res = Math.min(res, price + fromValue);}}}return res == Integer.MAX_VALUE ? -1 : res;}} 边栏推荐
- Introduction and use of apifox (1).
- Mysql return table
- goroutine (coroutine) in go language
- ELK日志分析系统
- MySQL 5.7 upgrade to 8.0 detailed process
- Mysql存储json格式数据
- MySQL String Concatenation - Various String Concatenation Practical Cases
- MySQL 的 limit 分页查询及性能问题
- go项目的打包部署
- MySQL 5.7 detailed download, installation and configuration tutorial
猜你喜欢
随机推荐
LeetCode刷题系列 -- 787. K 站中转内最便宜的航班
Mysql存储json格式数据
2022年100道最新软件测试面试题,常见面试题及答案汇总
el-input 只能输入整数(包括正数、负数、0)或者只能输入整数(包括正数、负数、0)和小数
自动化运维工具——ansible、概述、安装、模块介绍
How much does a test environment cost? Start with cost and efficiency
canvas 像素操作(图片像素操作)
CAN光端机解决泰和安TX3016C消防主机长距离联网问题 实现CAN与光纤之间的双向数据智能转换
MySql字符串拆分实现split功能(字段分割转列、转行)
2021年软件测试面试题大全
牛客-TOP101-BM41
Grid布局介绍
MySQL String Concatenation - Various String Concatenation Practical Cases
apisix-入门使用篇
classSR论文阅读笔记
apisix-Getting Started
MySQL 字符串拼接 - 多种字符串拼接实战案例
coredns介绍
[PSQL] window function, GROUPING operator
数据湖:流计算处理框架Flink概述









