Skip to content

Commit 96c0036

Browse files
authored
feat: add rust solution to lc problem: No.3407 (#4730)
1 parent 16fb183 commit 96c0036

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

solution/3400-3499/3407.Substring Matching Pattern/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,13 @@ tags:
8080

8181
<!-- solution:start -->
8282

83-
### 方法一
83+
### 方法一:字符串匹配
84+
85+
根据题目描述,`*` 可以被替换为零个或多个字符组成的任意字符序列,因此我们可以将模式字符串 $p$ 按照 `*` 分割成若干个子串,如果这些子串依次出现在字符串 $s$ 中且顺序不变,则说明 $p$ 可以变成 $s$ 的子字符串。
86+
87+
因此,我们首先初始化一个指针 $i$ 指向字符串 $s$ 的起始位置,然后遍历模式字符串 $p$ 按照 `*` 分割得到的每个子串 $t$,在字符串 $s$ 中从位置 $i$ 开始查找子串 $t$,如果找到了,则将指针 $i$ 移动到子串 $t$ 的末尾位置继续查找下一个子串;如果找不到,则说明模式字符串 $p$ 不能变成字符串 $s$ 的子字符串,返回 $\text{false}$。如果所有子串都找到了,则返回 $\text{true}$。
88+
89+
时间复杂度 $O(n \times m)$,空间复杂度 $O(m)$。其中 $n$ 和 $m$ 分别是字符串 $s$ 和模式字符串 $p$ 的长度。
8490

8591
<!-- tabs:start -->
8692

solution/3400-3499/3407.Substring Matching Pattern/README_EN.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ tags:
7878

7979
<!-- solution:start -->
8080

81-
### Solution 1
81+
### Solution 1: String Matching
82+
83+
According to the problem description, `*` can be replaced by any sequence of zero or more characters, so we can split the pattern string $p$ by `*` into several substrings. If these substrings appear in order in the string $s$, then $p$ can become a substring of $s$.
84+
85+
Therefore, we first initialize a pointer $i$ to the start of string $s$, then iterate over each substring $t$ obtained by splitting the pattern string $p$ by `*`. For each $t$, we search for it in $s$ starting from position $i$. If it is found, we move the pointer $i$ to the end of $t$ and continue searching for the next substring. If it is not found, it means the pattern string $p$ cannot become a substring of $s$, and we return $\text{false}$. If all substrings are found, we return $\text{true}$.
86+
87+
The time complexity is $O(n \times m)$, and the space complexity is $O(m)$, where $n$ and $m$ are the lengths of strings $s$ and $p$, respectively.
8288

8389
<!-- tabs:start -->
8490

@@ -174,6 +180,24 @@ function hasMatch(s: string, p: string): boolean {
174180
}
175181
```
176182

183+
#### Rust
184+
185+
```rust
186+
impl Solution {
187+
pub fn has_match(s: String, p: String) -> bool {
188+
let mut i = 0usize;
189+
for t in p.split('*') {
190+
if let Some(j) = s[i..].find(t) {
191+
i += j + t.len();
192+
} else {
193+
return false;
194+
}
195+
}
196+
true
197+
}
198+
}
199+
```
200+
177201
<!-- tabs:end -->
178202

179203
<!-- solution:end -->
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn has_match(s: String, p: String) -> bool {
3+
let mut i = 0usize;
4+
for t in p.split('*') {
5+
if let Some(j) = s[i..].find(t) {
6+
i += j + t.len();
7+
} else {
8+
return false;
9+
}
10+
}
11+
true
12+
}
13+
}

0 commit comments

Comments
 (0)