-
Notifications
You must be signed in to change notification settings - Fork 612
/
1078.py
44 lines (34 loc) · 1.31 KB
/
1078.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'''
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 <= text.length <= 1000
text consists of space separated words, where each word consists of lowercase English letters.
1 <= first.length, second.length <= 10
first and second consist of lowercase English letters.
'''
class Solution(object):
def findOcurrences(self, text, first, second):
"""
:type text: str
:type first: str
:type second: str
:rtype: List[str]
"""
result = []
if not text:
return []
splitted_text = text.split(' ')
indi = 0
for index in range(len(splitted_text)-1):
if splitted_text[index] == first and splitted_text[index+1] == second:
index = index+2
if index < len(splitted_text):
result.append(splitted_text[index])
return result