加载中...
1078-Bigram 分词(Occurrences After Bigram)
发表于:2021-12-03 | 分类: 简单
字数统计: 469 | 阅读时长: 2分钟 | 阅读量:

原文链接: 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 <= 1000
  • text consists of lowercase English letters and spaces.
  • All the words in text a separated by a single space.
  • 1 <= first.length, second.length <= 10
  • first and second consist 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. 1 <= text.length <= 1000
  2. text 由一些用空格分隔的单词组成,每个单词都由小写英文字母组成
  3. 1 <= first.length, second.length <= 10
  4. first 和 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%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
1072-按列翻转得到最大值等行数(Flip Columns For Maximum Number of Equal Rows)
下一篇:
1080-根到叶路径上的不足节点(Insufficient Nodes in Root to Leaf Paths)
本文目录
本文目录