Skip to content

Commit 9fec2a9

Browse files
author
Zhang Xiaodong
committed
solve 44
1 parent c1c681f commit 9fec2a9

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ mod s0040_combination_sum_ii;
3939
mod s0041_first_missing_positive;
4040
mod s0042_trapping_rain_water;
4141
mod s0043_multiply_strings;
42+
mod s0044_wildcard_matching;
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* [44] 通配符匹配
3+
*
4+
* <div class="title__3Vvk">给你一个输入字符串 (s) 和一个字符模式 (p) ,请你实现一个支持 '?''*' 匹配规则的通配符匹配:</div>
5+
*
6+
* <li class="title__3Vvk">'?' 可以匹配任何单个字符。
7+
* <li class="title__3Vvk">'*' 可以匹配任意字符序列(包括空字符序列)。
8+
*
9+
* <div class="original__bRMd">
10+
* <div>
11+
* 判定匹配成功的充要条件是:字符模式必须能够 完全匹配 输入字符串(而不是部分匹配)。
12+
* </div>
13+
* </div>
14+
*
15+
* <strong class="example">示例 1
16+
*
17+
* 输入:s = "aa", p = "a"
18+
* 输出:false
19+
* 解释:"a" 无法匹配 "aa" 整个字符串。
20+
*
21+
* <strong class="example">示例 2
22+
*
23+
* 输入:s = "aa", p = "*"
24+
* 输出:true
25+
* 解释:'*' 可以匹配任意字符串。
26+
*
27+
* <strong class="example">示例 3
28+
*
29+
* 输入:s = "cb", p = "?a"
30+
* 输出:false
31+
* 解释:'?' 可以匹配 'c', 但第二个 'a' 无法匹配 'b'
32+
*
33+
*
34+
* 提示:
35+
*
36+
* 0 <= s.length, p.length <= 2000
37+
* s 仅由小写英文字母组成
38+
* p 仅由小写英文字母、'?''*' 组成
39+
*
40+
*/
41+
pub struct Solution {}
42+
43+
// problem: https://leetcode.cn/problems/wildcard-matching/
44+
// discuss: https://leetcode.cn/problems/wildcard-matching/discuss/?currentPage=1&orderBy=most_votes&query=
45+
46+
// submission codes start here
47+
48+
impl Solution {
49+
pub fn is_match(s: String, p: String) -> bool {
50+
let s = s.as_bytes();
51+
let p = p.as_bytes();
52+
let mut dp: Vec<Vec<bool>> = vec![vec![false; p.len()+1]; s.len()+1];
53+
dp[0][0] = true;
54+
for j in 1..=p.len() {
55+
if p[j-1] == b'*' {
56+
dp[0][j] = true;
57+
} else {
58+
break;
59+
}
60+
}
61+
for i in 1..=s.len() {
62+
for j in 1..=p.len() {
63+
match p[j-1] {
64+
b'?' => {
65+
dp[i][j] = dp[i-1][j-1];
66+
},
67+
b'*' => {
68+
dp[i][j] = dp[i-1][j] | dp[i][j-1];
69+
},
70+
_ => {
71+
if s[i-1] == p[j-1] {
72+
dp[i][j] = dp[i-1][j-1];
73+
}
74+
},
75+
}
76+
}
77+
}
78+
dp[s.len()][p.len()]
79+
}
80+
}
81+
82+
// submission codes end
83+
84+
#[cfg(test)]
85+
mod tests {
86+
use super::*;
87+
88+
#[test]
89+
fn test_44() {
90+
assert_eq!(Solution::is_match("aa".to_string(), "a".to_string()), false);
91+
assert_eq!(Solution::is_match("aa".to_string(), "*".to_string()), true);
92+
assert_eq!(Solution::is_match("hello wold".to_string(), "he*w?ld".to_string()), true);
93+
assert_eq!(Solution::is_match("cb".to_string(), "?a".to_string()), false);
94+
assert_eq!(Solution::is_match("".to_string(), "****".to_string()), true);
95+
assert_eq!(Solution::is_match("acdcb".to_string(), "a*c?b".to_string()), false);
96+
assert_eq!(Solution::is_match("mississippi".to_string(), "m??*ss*?i*pi".to_string()), false);
97+
assert_eq!(Solution::is_match("bbbbbbbabbaabbabbbbaaabbabbabaaabbababbbabbbabaaabaab".to_string(), "b*b*ab**ba*b**b***bba".to_string()), false);
98+
}
99+
}

0 commit comments

Comments
 (0)