原文链接: https://leetcode-cn.com/problems/occurrences-after-bigram
英文原文
Given two strings first and second, consider occurrences in some text of the form "first second third", where second comes immediately after first, and third comes immediately after second.
Return an array of all the words third for each occurrence of "first second third".
Example 1:
Input: text = "alice is a good girl she is a good student", first = "a", second = "good" Output: ["girl","student"]
Example 2:
Input: text = "we will we will rock you", first = "we", second = "will" Output: ["we","rock"]
Constraints:
1 <= text.length <= 1000textconsists of lowercase English letters and spaces.- All the words in
texta separated by a single space. 1 <= first.length, second.length <= 10firstandsecondconsist of lowercase English letters.
中文题目
给出第一个词 first 和第二个词 second,考虑在某些文本 text 中可能以 "first second third" 形式出现的情况,其中 second 紧随 first 出现,third 紧随 second 出现。
对于每种这样的情况,将第三个词 "third" 添加到答案中,并返回答案。
示例 1:
输入:text = "alice is a good girl she is a good student", first = "a", second = "good" 输出:["girl","student"]
示例 2:
输入:text = "we will we will rock you", first = "we", second = "will" 输出:["we","rock"]
提示:
1 <= text.length <= 1000text由一些用空格分隔的单词组成,每个单词都由小写英文字母组成1 <= first.length, second.length <= 10first和second由小写英文字母组成
通过代码
高赞题解
1.split
2.zip(text, text[1:], text[2:])
3.列表推导式
class Solution:
def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
text = text.split()
return [trd for fst, sed, trd in zip(text, text[1:], text[2:]) if fst == first and sed == second]
统计信息
| 通过次数 | 提交次数 | AC比率 |
|---|---|---|
| 12223 | 19682 | 62.1% |
提交历史
| 提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
|---|