当前位置:网站首页>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 matches
The 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;}}
边栏推荐
猜你喜欢
随机推荐
Mysql implements optimistic locking
MySql将一张表的数据copy到另一张表中
Go语学习笔记 - 处理超时问题 - Context使用 从零开始Go语言
从DES走到AES(现代密码的传奇之路)
MySQL如何对SQL做prepare预处理(解决IN查询SQL预处理仅能查询出一条记录的问题)
Detailed explanation of mysql stored procedure
[Digital IC hand-tear code] Verilog fixed priority arbiter | topic | principle | design | simulation
Matlab paper illustration drawing template No. 41 - bubble chart (bubblechart)
Redis数据库
navicat connects to MySQL and reports an error: 1045 - Access denied for user 'root'@'localhost' (using password YES)
利用浏览器本地存储 实现记住用户名的功能
12 reasons for MySQL slow query
What do interview test engineers usually ask?The test supervisor tells you
MySQL安装常见报错处理大全
公司不重视软件测试,新来的阿里P8给我们撰写了测试用例编写规范
认识消防报警联网中CAN光纤转换器的光纤接口和配套光纤线缆
Three methods of importing sql files in MySQL
swinIR论文阅读笔记
Android studio连接MySQL并完成简单的登录注册功能
ELK log analysis system