관리 메뉴

솜씨좋은장씨

[leetCode] 1078. Occurrences After Bigram (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 1078. Occurrences After Bigram (Python)

솜씨좋은장씨 2020. 10. 21. 21:52
728x90
반응형

Given words 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.

For each such occurrence, add "third" to the answer, and return the answer.

 

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"]

 

Note:

  1. 1 <= text.length <= 1000
  2. text consists of space separated words, where each word consists of lowercase English letters.
  3. 1 <= first.length, second.length <= 10
  4. first and second consist of lowercase English letters.

Solution

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

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

Comments