Day 30 - Occurrences After Bigram

大家好,我是毛毛。ヾ(´∀ ˋ)ノ
来到30天的最後一天解题Day啦~


1078. Occurrences After Bigram

Question

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

Example1

Input: text = "alice is a good girl she is a good student", first = "a", second = "good"
Output: ["girl","student"]

Example2

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.

解题

题目

首先先简单的翻译一下题目
给一个字串句子,然後给firstsecond两个字串,要找到所有符合条件的third字串,条件就是first下一个要接着second,second下一个要接third。

Think

作法大致上是这样

  • 就照着上面的想法,每个index都往下两个index判断是否符合。
  • 感觉改成while,让符合条件存入list的时候让index直接跳到下下一个位置的index可能可以再缩短一点执行时间。

Code

Python

class Solution:
    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
        list_text = text.split(" ")
        ans = []
        
        for index in range(len(list_text)):
            
            if list_text[index] == first:
                if index+1 <= len(list_text)-1 and list_text[index+1] == second:
                    if index+2 <= len(list_text)-1:
                        ans.append(list_text[index+2])
        
        return ans

Result

  • Python

大家下次见/images/emoticon/emoticon29.gif


<<:  Youtube API - 凡事趁早没有那麽多来日方长(总结)

>>:  Day 30: 人工智慧在音乐领域的应用 (总结)

[Day04] Tableau 轻松学 - Tableau 三大软件

前言 Tableau 是多个软件的通称,我们初学在进行 BI 分析时,最常会用到其中的三种,分别为 ...

Day 12:vim 配色方案

俗话说人要衣装,佛要金装,我们的 vim 也得要有漂亮的外观。今天就让我们来看看如何调教调整 vim...

铁人赛 Day14-- MySQL函式 -- 查询资料分页(我傻眼,昨天没上传到)

前言 我明明记得我昨天就PO==,结果今天看到通知才发现被中断了,算了哈哈,那我这边就慢慢PO好了,...

Day 21 ASP.NET Core Identity

今天来实作身分验证的部分,笔者此前是用 ASP.NET Core Web API 搭配 Blazor...