1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence 链接到标题 切分句子然后遍历判断是否为前缀,需要返回索引 + 1。
class Solution: def isPrefixOfWord(self, sentence: str, searchWord: str) -> int: for idx, w in enumerate(sentence.split(' ')): if w.startswith(searchWord): return idx + 1 else: return -1 1456. Maximum Number of Vowels in a Substring of Given Length 链接到标题 滑动窗口,使用字典统计每个元音出现的次数,每次更新最大值。
class Solution: def maxVowels(self, s: str, k: int) -> int: d = {'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0} for c in s[:k]: if c in d: d[c] += 1 res = sum(d.