当前位置: 首页 > wzjs >正文

wordpress 段落美化郴州seo快速排名

wordpress 段落美化,郴州seo快速排名,网站站群优化,网站建设咨询服务合同【LetMeFly】2255.统计是给定字符串前缀的字符串数目:使用库函数计数 力扣题目链接:https://leetcode.cn/problems/count-prefixes-of-a-given-string/ 给你一个字符串数组 words 和一个字符串 s ,其中 words[i] 和 s 只包含 小写英文字母 …

【LetMeFly】2255.统计是给定字符串前缀的字符串数目:使用库函数+计数

力扣题目链接:https://leetcode.cn/problems/count-prefixes-of-a-given-string/

给你一个字符串数组 words 和一个字符串 s ,其中 words[i] 和 s 只包含 小写英文字母 。

请你返回 words 中是字符串 s 前缀 字符串数目 。

一个字符串的 前缀 是出现在字符串开头的子字符串。子字符串 是一个字符串中的连续一段字符序列。

 

示例 1:

输入:words = ["a","b","c","ab","bc","abc"], s = "abc"
输出:3
解释:
words 中是 s = "abc" 前缀的字符串为:
"a" ,"ab" 和 "abc" 。
所以 words 中是字符串 s 前缀的字符串数目为 3 。

示例 2:

输入:words = ["a","a"], s = "aa"
输出:2
解释:
两个字符串都是 s 的前缀。
注意,相同的字符串可能在 words 中出现多次,它们应该被计数多次。

 

提示:

  • 1 <= words.length <= 1000
  • 1 <= words[i].length, s.length <= 10
  • words[i] 和 s  包含小写英文字母。

解题方法:使用库函数+计数

很多编程语言都有判断一个字符串word是否为另一个字符串s的前缀的函数:

  • C++s.find(word) == 0
  • Pythons.startswith(word)
  • Javas.startsWith(word)
  • Golangstrings.HasPrefix(s, word)

计数即为统计words中有多少个words的前缀:

使用一个变量ans,初始值为0,在遍历words字符串数组的时候更新ans值就行了。

  • 时间复杂度 O ( m n ) O(mn) O(mn),其中 m = l e n ( w o r d s ) , n = l e n ( s ) m=len(words), n=len(s) m=len(words),n=len(s)
  • 空间复杂度 O ( 1 ) O(1) O(1)

AC代码

C++
/** @Author: LetMeFly* @Date: 2025-03-24 17:41:54* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-03-24 17:51:42*/
class Solution {
public:int countPrefixes(vector<string>& words, string s) {int ans = 0;for (string& word : words) {ans += s.find(word) == 0;}return ans;}
};
Python
'''
Author: LetMeFly
Date: 2025-03-24 17:52:11
LastEditors: LetMeFly.xyz
LastEditTime: 2025-03-24 17:52:19
'''
from typing import Listclass Solution:def countPrefixes(self, words: List[str], s: str) -> int:return sum(s.startswith(word) for word in words)
Java
/** @Author: LetMeFly* @Date: 2025-03-24 17:53:35* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-03-24 17:53:41*/
class Solution {public int countPrefixes(String[] words, String s) {int ans = 0;for (String word : words) {ans += s.startsWith(word) ? 1 : 0;}return ans;}
}
Go
/** @Author: LetMeFly* @Date: 2025-03-24 17:55:08* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-03-24 17:55:24*/
package mainimport "strings"func countPrefixes(words []string, s string) (ans int) {for _, word := range words {if strings.HasPrefix(s, word) {ans++}}return
}

同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

千篇源码题解已开源


文章转载自:

http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://00000000.xkzrt.cn
http://www.dtcms.com/wzjs/604657.html

相关文章:

  • asp.net做简易网站品牌企业网站建设公司价格
  • 青岛网站建设报价网络销售是做网站推广
  • 视频网站怎么做排名全球军情动态
  • 智能建站是什么注册城乡规划师考试
  • 做家装的网站好邢台学校网站建设价格
  • 浏览不良网站会被网警抓吗做快三网站
  • 盐城网站定制模板网站和定制网站
  • 零基础 网站汽车之家网页版
  • 金融公司网站规划方案国外企业招聘网站
  • 信托公司网站建设windows搭建php网站
  • 网站建设目的确定龙岩有什么招聘本地网站
  • 网站左侧悬浮安徽省建设厅网站证书查询
  • 做微网站的公司哪家好营销网站建设专业服务公司
  • 万网空间上传网站吗北京外贸网站优化
  • 滨州建设工程备案网站wordpress可爱主题下载
  • 河南企业网站定制iis网站怎么做全站伪静态
  • 台州建设网站免费推广策略
  • 备案我网站的大致内容是网站做美食视频挣钱吗
  • 网路神做网站怎么样建设mylove卡网站
  • 开源asp学校系统网站爱奇艺的网站是用什么做的
  • 怎么在百度搜索自己的网站网站系统环境的搭建
  • 公司网站维护网站如何做排名
  • 哪里可以免费建网站wordpress博客软件
  • 网站开发成本如何入账树状结构的网站
  • 收到网站打入0.1元怎么做分录长春企业网站设计
  • 圣辉友联做网站公司陕西省建设网官网陕西省建筑市场监督与诚信信息一体化平台
  • 邯郸网站建设taigew单页网站制作需要多少钱
  • 云邦北京网站建设建设银行纪检监察网站
  • 女装网站欣赏无锡百度关键词优化
  • 有哪些可以做头像的网站大型门户网站程序