网站 建设 汇报,标识标牌网站怎么做,郑州网站推广价格,浅议我国旅游景点网站的建设文章目录1. 题目2. 解题1. 题目
给你一个字符串数组 words 和一个字符串 pref 。
返回 words 中以 pref 作为 前缀 的字符串的数目。
字符串 s 的 前缀 就是 s 的任一前导连续字符串。
示例 1#xff1a;
输入#xff1a;words [pay,attention,…
文章目录1. 题目2. 解题1. 题目
给你一个字符串数组 words 和一个字符串 pref 。
返回 words 中以 pref 作为 前缀 的字符串的数目。
字符串 s 的 前缀 就是 s 的任一前导连续字符串。
示例 1
输入words [pay,attention,practice,attend], pref at
输出2
解释以 at 作为前缀的字符串有两个分别是attention 和 attend 。示例 2
输入words [leetcode,win,loops,success], pref code
输出0
解释不存在以 code 作为前缀的字符串。提示
1 words.length 100
1 words[i].length, pref.length 100
words[i] 和 pref 由小写英文字母组成来源力扣LeetCode 链接https://leetcode-cn.com/problems/counting-words-with-a-given-prefix 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。 2. 解题
class Solution:def prefixCount(self, words: List[str], pref: str) - int:return len([x for x in words if x.startswith(pref)])36 ms 15.1 MB Python3
class Solution {
public:int prefixCount(vectorstring words, string pref) {int n pref.size(), ans 0;for(auto w : words){if(w.size() n) continue;if(w.substr(0,n) pref)ans;}return ans;}
};12 ms 9.7 MB C 我的CSDN博客地址 https://michael.blog.csdn.net/
长按或扫码关注我的公众号Michael阿明一起加油、一起学习进步