当前位置:网站首页>Leetcode 44 Wildcard matching (2022.02.13)
Leetcode 44 Wildcard matching (2022.02.13)
2022-07-06 00:33:00 【ChaoYue_ miku】
Given a string (s) And a character pattern § , Implement a support ‘?’ and ‘*’ The wildcard matches .
‘?’ It can match any single character .
‘*’ Can match any string ( Include empty string ).
Only when two strings match exactly can the match be successful .
explain :
s May is empty , And contains only from a-z Lowercase letters of .
p May is empty , And contains only from a-z Lowercase letters of , As well as the character ? and *.
Example 1:
Input :
s = “aa”
p = “a”
Output : false
explain : “a” Can't match “aa” Whole string .
Example 2:
Input :
s = “aa”
p = ""
Output : true
explain : '’ Can match any string .
Example 3:
Input :
s = “cb”
p = “?a”
Output : false
explain : ‘?’ Can match ‘c’, But the second one ‘a’ Can't match ‘b’.
Example 4:
Input :
s = “adceb”
p = “ab”
Output : true
explain : first ‘’ Can match an empty string , the second '’ Can match string “dce”.
Example 5:
Input :
s = “acdcb”
p = “a*c?b”
Output : false
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/wildcard-matching
Method 1 : Dynamic programming
C++ Submission :
class Solution {
public:
bool isMatch(string s, string p) {
int m = s.size();
int n = p.size();
vector<vector<int>> dp(m + 1, vector<int>(n + 1));
dp[0][0] = true;
for (int i = 1; i <= n; ++i) {
if (p[i - 1] == '*') {
dp[0][i] = true;
}else {
break;
}
}
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (p[j - 1] == '*') {
dp[i][j] = dp[i][j - 1] | dp[i - 1][j];
}else if (p[j - 1] == '?' || s[i - 1] == p[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
}
}
}
return dp[m][n];
}
};
边栏推荐
- 多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
- Free chat robot API
- Spark获取DataFrame中列的方式--col,$,column,apply
- 建立时间和保持时间的模型分析
- Analysis of the combination of small program technology advantages and industrial Internet
- PHP determines whether an array contains the value of another array
- Leetcode:20220213 week race (less bugs, top 10% 555)
- AtCoder Beginner Contest 254【VP记录】
- JS can really prohibit constant modification this time!
- An understanding of & array names
猜你喜欢
Multithreading and high concurrency (8) -- summarize AQS shared lock from countdownlatch (punch in for the third anniversary)
Spark SQL null value, Nan judgment and processing
Basic introduction and source code analysis of webrtc threads
Go learning - dependency injection
MySql——CRUD
Mysql - CRUD
STM32 configuration after chip replacement and possible errors
How to use the flutter framework to develop and run small programs
State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
Data analysis thinking analysis methods and business knowledge -- analysis methods (II)
随机推荐
Notepad + + regular expression replace String
Room cannot create an SQLite connection to verify the queries
Problems and solutions of converting date into specified string in date class
[designmode] composite mode
STM32按键消抖——入门状态机思维
Data analysis thinking analysis methods and business knowledge - analysis methods (III)
小程序技术优势与产业互联网相结合的分析
XML Configuration File
N1 # if you work on a metauniverse product [metauniverse · interdisciplinary] Season 2 S2
Classic CTF topic about FTP protocol
Global and Chinese markets of POM plastic gears 2022-2028: Research Report on technology, participants, trends, market size and share
Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)
Search (DFS and BFS)
The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share
MySql——CRUD
Yolov5、Pycharm、Anaconda环境安装
MySql——CRUD
数据分析思维分析方法和业务知识——分析方法(二)
OpenCV经典100题
Spark SQL null value, Nan judgment and processing