「每日LeetCode」2022年4月1日

本文最后更新于:2023年3月19日 晚上

2185.统计包含给定前缀的字符串

2185.统计包含给定前缀的字符串

Category Difficulty Likes Dislikes
algorithms Easy (78.94%) 8 -

Tags
Companies
给你一个字符串数组 words 和一个字符串 pref 。
返回 words_ _中以 pref 作为 前缀 的字符串的数目。
字符串 s 的 前缀 就是 s 的任一前导连续字符串。

示例 1:
输入:words = [“pay”,”attention”,”practice”,”attend”], pref = “at” 输出:2 解释:以 “at” 作为前缀的字符串有两个,分别是:”at_tention” 和 “at_tend” 。
示例 2:
输入:words = [“leetcode”,”win”,”loops”,”success”], pref = “code” 输出:0 解释:不存在以 “code” 作为前缀的字符串。

提示:

  • 1 <= words.length <= 100
  • 1 <= words[i].length, pref.length <= 100
  • words[i] 和 pref 由小写英文字母组成

Discussion | Solution

思路

按题意模拟即可

解答

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
* @lc app=leetcode.cn id=2185 lang=javascript
*
* [2185] 统计包含给定前缀的字符串
*/

// @lc code=start
/**
* @param {string[]} words
* @param {string} pref
* @return {number}
*/
var prefixCount = function (words, pref) {
return words.filter((word) => word.startsWith(pref)).length;
};
// @lc code=end