Skip to content

[pull] main from doocs:main #439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions solution/2200-2299/2255.Count Prefixes of a Given String/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ words 中是 s = "abc" 前缀的字符串为:

### 方法一:遍历计数

我们直接遍历数组 $words$,对于每个字符串 $w$,判断 $s$ 是否以 $w$ 为前缀,如果是则答案加一。
我们直接遍历数组 $\textit{words}$,对于每个字符串 $w$,判断 $s$ 是否以 $w$ 为前缀,如果是则答案加一。

遍历结束后,返回答案即可。

时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是数组 $words$ 的长度和字符串 $s$ 的长度。空间复杂度 $O(1)$。
时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是数组 $\textit{words}$ 的长度和字符串 $s$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -130,6 +130,26 @@ function countPrefixes(words: string[], s: string): number {
}
```

#### Rust

```rust
impl Solution {
pub fn count_prefixes(words: Vec<String>, s: String) -> i32 {
words.iter().filter(|w| s.starts_with(w.as_str())).count() as i32
}
}
```

#### C#

```cs
public class Solution {
public int CountPrefixes(string[] words, string s) {
return words.Count(w => s.StartsWith(w));
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Thus the number of strings in words which are a prefix of s is 3.</pre>
<strong>Input:</strong> words = [&quot;a&quot;,&quot;a&quot;], s = &quot;aa&quot;
<strong>Output:</strong> 2
<strong>Explanation:
</strong>Both of the strings are a prefix of s.
</strong>Both of the strings are a prefix of s.
Note that the same string can occur multiple times in words, and it should be counted each time.</pre>

<p>&nbsp;</p>
Expand Down Expand Up @@ -130,6 +130,26 @@ function countPrefixes(words: string[], s: string): number {
}
```

#### Rust

```rust
impl Solution {
pub fn count_prefixes(words: Vec<String>, s: String) -> i32 {
words.iter().filter(|w| s.starts_with(w.as_str())).count() as i32
}
}
```

#### C#

```cs
public class Solution {
public int CountPrefixes(string[] words, string s) {
return words.Count(w => s.StartsWith(w));
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class Solution {
public int CountPrefixes(string[] words, string s) {
return words.Count(w => s.StartsWith(w));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
impl Solution {
pub fn count_prefixes(words: Vec<String>, s: String) -> i32 {
words.iter().filter(|w| s.starts_with(w.as_str())).count() as i32
}
}