2185.统计包含给定前缀的字符串
链接:2185.统计包含给定前缀的字符串
难度:Easy
标签:数组、字符串、字符串匹配
简介:给你一个字符串数组 words 和一个字符串 pref 。返回 words 中以 pref 作为 前缀 的字符串的数目。
题解 1 - rust
- 编辑时间:2023-01-08
- 内存消耗:2.1MB
- 编程语言:rust
- 解法介绍:同上。
impl Solution {
pub fn prefix_count(words: Vec<String>, pref: String) -> i32 {
words.into_iter().filter(|w| w.starts_with(&pref)).count() as i32
}
}
题解 2 - typescript
- 编辑时间:2023-01-08
- 执行用时:56ms
- 内存消耗:44.1MB
- 编程语言:typescript
- 解法介绍:遍历。
function prefixCount(words: string[], pref: string): number {
return words.filter(v => v.startsWith(pref)).length;
}