Skip to content

Commit 1fc775e

Browse files
committed
feat: add solutions to lc problem: No.2185
No.2185.Counting Words With a Given Prefix
1 parent b9eb7de commit 1fc775e

File tree

5 files changed

+62
-18
lines changed

5 files changed

+62
-18
lines changed

solution/2100-2199/2185.Counting Words With a Given Prefix/README.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,29 @@ func prefixCount(words []string, pref string) int {
291291

292292
```ts
293293
function prefixCount(words: string[], pref: string): number {
294-
const m = pref.length;
295-
let ans = 0;
296-
for (const w of words) {
297-
if (w.substring(0, m) === pref) {
298-
++ans;
294+
return words.reduce((r, s) => (r += s.startsWith(pref) ? 1 : 0), 0);
295+
}
296+
```
297+
298+
### **Rust**
299+
300+
```rust
301+
impl Solution {
302+
pub fn prefix_count(words: Vec<String>, pref: String) -> i32 {
303+
words.iter().filter(|s| s.starts_with(&pref)).count() as i32
304+
}
305+
}
306+
```
307+
308+
### **C**
309+
310+
```c
311+
int prefixCount(char **words, int wordsSize, char *pref) {
312+
int ans = 0;
313+
int n = strlen(pref);
314+
for (int i = 0; i < wordsSize; i++) {
315+
if (strncmp(words[i], pref, n) == 0) {
316+
ans++;
299317
}
300318
}
301319
return ans;

solution/2100-2199/2185.Counting Words With a Given Prefix/README_EN.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,29 @@ func prefixCount(words []string, pref string) int {
257257

258258
```ts
259259
function prefixCount(words: string[], pref: string): number {
260-
const m = pref.length;
261-
let ans = 0;
262-
for (const w of words) {
263-
if (w.substring(0, m) === pref) {
264-
++ans;
260+
return words.reduce((r, s) => (r += s.startsWith(pref) ? 1 : 0), 0);
261+
}
262+
```
263+
264+
### **Rust**
265+
266+
```rust
267+
impl Solution {
268+
pub fn prefix_count(words: Vec<String>, pref: String) -> i32 {
269+
words.iter().filter(|s| s.starts_with(&pref)).count() as i32
270+
}
271+
}
272+
```
273+
274+
### **C**
275+
276+
```c
277+
int prefixCount(char **words, int wordsSize, char *pref) {
278+
int ans = 0;
279+
int n = strlen(pref);
280+
for (int i = 0; i < wordsSize; i++) {
281+
if (strncmp(words[i], pref, n) == 0) {
282+
ans++;
265283
}
266284
}
267285
return ans;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
int prefixCount(char **words, int wordsSize, char *pref) {
2+
int ans = 0;
3+
int n = strlen(pref);
4+
for (int i = 0; i < wordsSize; i++) {
5+
if (strncmp(words[i], pref, n) == 0) {
6+
ans++;
7+
}
8+
}
9+
return ans;
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
impl Solution {
2+
pub fn prefix_count(words: Vec<String>, pref: String) -> i32 {
3+
words.iter().filter(|s| s.starts_with(&pref)).count() as i32
4+
}
5+
}
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
11
function prefixCount(words: string[], pref: string): number {
2-
const m = pref.length;
3-
let ans = 0;
4-
for (const w of words) {
5-
if (w.substring(0, m) === pref) {
6-
++ans;
7-
}
8-
}
9-
return ans;
2+
return words.reduce((r, s) => (r += s.startsWith(pref) ? 1 : 0), 0);
103
}

0 commit comments

Comments
 (0)