2255-startwith实践出真知
给你一个字符串数组 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 。
class Solution(object): def countPrefixes(self, words, s): """ :type words: List[str] :type s: str :rtype: int """ ans = 0 target = len(s) for word in words: length = len(word) if length <= target: if(word == s[0:length]): ans = ans + 1 return ans

看了题解发现有 startwith 其实之前也知道 但是不知道能判断这种子字符串 以为'abc'只能是'abc' 还是实践出真知
Work Hard
But do not forget to enjoy life😀
本文来自博客园,作者:YuhangLiuCE,转载请注明原文链接:https://www.cnblogs.com/YuhangLiuCE/p/17821796.html
posted on 2023-11-09 15:24 YuhangLiuCE 阅读(57) 评论(0) 收藏 举报
浙公网安备 33010602011771号