当前位置:网站首页>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;}} 边栏推荐
猜你喜欢
随机推荐
21天学习挑战赛安排
元空间内存溢出
el-input 只能输入整数(包括正数、负数、0)或者只能输入整数(包括正数、负数、0)和小数
navicat新建数据库
golang的time包:时间间隔格式化和秒、毫秒、纳秒等时间戳格式输出的方法
CAN光端机解决泰和安TX3016C消防主机长距离联网问题 实现CAN与光纤之间的双向数据智能转换
go项目的打包部署
MySQL如何对SQL做prepare预处理(解决IN查询SQL预处理仅能查询出一条记录的问题)
MYSQL unique constraint
apisix-Getting Started
The original question on the two sides of the automatic test of the byte beating (arranged according to the recording) is real and effective 26
【HCIE】NO.45 Hub and Spoke配置案例
Redis常见题型
MySQL大批量造数据
el-input can only input integers (including positive numbers, negative numbers, 0) or only integers (including positive numbers, negative numbers, 0) and decimals
合作的小伙伴,缺乏主人翁(owner)意识,好苦恼
12 reasons for MySQL slow query
MySQL 字符串拼接 - 多种字符串拼接实战案例
MySQL 的 limit 分页查询及性能问题
Review: image saturation calculation formula and image signal-to-noise (PSNR) ratio calculation formula









