2185. 统计包含给定前缀的字符串
难度简单28
给你一个字符串数组 words 和一个字符串 pref 。
返回 words 中以 pref 作为 前缀 的字符串的数目。
字符串 s 的 前缀 就是 s 的任一前导连续字符串。
示例 1:
pref
示例 2:
pref
提示:
-
1 <= words.length <= 100 -
1 <= words[i].length, pref.length <= 100 -
words[i] 和 pref 由小写英文字母组成
Solution
今天开了京东会员,然后在咸鱼上疯狂卖视频会员回本,遇到的人都很好,爱了。
然后今天没打周赛,明天科目三,希望能够顺利通关,麻了。
今天这题是水题,但是有个使用了闭包的函数starts_with(),现在还不会用,不得不说闭包真的能让代码变得简洁。
代码(Rust)
impl Solution {
pub fn prefix_count(words: Vec<String>, pref: String) -> i32 {
let n = pref.len();
let mut res: i32 = 0;
for word in words {
if word.len() >= n && word[..n] == pref {
res += 1;
}
}
res
}
}










