-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwildcard_matching.rs
65 lines (62 loc) · 1.63 KB
/
wildcard_matching.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
///
/// Problem: Wildcard Matching
///
/// Given an input string `s` and a pattern `p`, implement wildcard pattern matching with support for:
/// - `'?'` Matches **any** single character.
/// - `'*'` Matches **any sequence** of characters (including the empty sequence).
///
/// **Example 1:**
/// ```plaintext
/// Input: s = "aa", p = "a"
/// Output: false
/// ```
///
/// **Example 2:**
/// ```plaintext
/// Input: s = "aa", p = "*"
/// Output: true
/// ```
///
/// **Example 3:**
/// ```plaintext
/// Input: s = "cb", p = "?a"
/// Output: false
/// ```
///
/// **Constraints:**
/// - `0 <= s.length, p.length <= 2000`
/// - `s` contains only lowercase English letters.
/// - `p` contains only lowercase English letters, '?' or '*'.
///
/// # Solution:
///
/// - **Time Complexity:** `O(m * n)`
/// - **Space Complexity:** `O(1)`
impl Solution {
pub fn is_match(s: String, p: String) -> bool {
let (mut i, mut j) = (0, 0);
let (mut star_idx, mut match_idx) = (-1, 0);
let s = s.as_bytes();
let p = p.as_bytes();
while i < s.len() {
if j < p.len() && (p[j] == b'?' || s[i] == p[j]) {
i += 1;
j += 1;
} else if j < p.len() && p[j] == b'*' {
star_idx = j as isize;
match_idx = i;
j += 1;
} else if star_idx != -1 {
j = (star_idx + 1) as usize;
match_idx += 1;
i = match_idx;
} else {
return false;
}
}
while j < p.len() && p[j] == b'*' {
j += 1;
}
j == p.len()
}
}