Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2737a8b

Browse files
feat: add solutions to lc problem: No.2255 (#4291)
No.2255.Count Prefixes of a Given String
1 parent 34f5f0c commit 2737a8b

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

‎solution/2200-2299/2255.Count Prefixes of a Given String/README.md‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ words 中是 s = "abc" 前缀的字符串为:
6262

6363
### 方法一:遍历计数
6464

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

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

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

7171
<!-- tabs:start -->
7272

@@ -130,6 +130,26 @@ function countPrefixes(words: string[], s: string): number {
130130
}
131131
```
132132

133+
#### Rust
134+
135+
```rust
136+
impl Solution {
137+
pub fn count_prefixes(words: Vec<String>, s: String) -> i32 {
138+
words.iter().filter(|w| s.starts_with(w.as_str())).count() as i32
139+
}
140+
}
141+
```
142+
143+
#### C#
144+
145+
```cs
146+
public class Solution {
147+
public int CountPrefixes(string[] words, string s) {
148+
return words.Count(w => s.StartsWith(w));
149+
}
150+
}
151+
```
152+
133153
<!-- tabs:end -->
134154

135155
<!-- solution:end -->

‎solution/2200-2299/2255.Count Prefixes of a Given String/README_EN.md‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Thus the number of strings in words which are a prefix of s is 3.</pre>
4242
<strong>Input:</strong> words = [&quot;a&quot;,&quot;a&quot;], s = &quot;aa&quot;
4343
<strong>Output:</strong> 2
4444
<strong>Explanation:
45-
</strong>Both of the strings are a prefix of s.
45+
</strong>Both of the strings are a prefix of s.
4646
Note that the same string can occur multiple times in words, and it should be counted each time.</pre>
4747

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

133+
#### Rust
134+
135+
```rust
136+
impl Solution {
137+
pub fn count_prefixes(words: Vec<String>, s: String) -> i32 {
138+
words.iter().filter(|w| s.starts_with(w.as_str())).count() as i32
139+
}
140+
}
141+
```
142+
143+
#### C#
144+
145+
```cs
146+
public class Solution {
147+
public int CountPrefixes(string[] words, string s) {
148+
return words.Count(w => s.StartsWith(w));
149+
}
150+
}
151+
```
152+
133153
<!-- tabs:end -->
134154

135155
<!-- solution:end -->
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Solution {
2+
public int CountPrefixes(string[] words, string s) {
3+
return words.Count(w => s.StartsWith(w));
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
impl Solution {
2+
pub fn count_prefixes(words: Vec<String>, s: String) -> i32 {
3+
words.iter().filter(|w| s.starts_with(w.as_str())).count() as i32
4+
}
5+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /