查看原题 https://leetcode-cn.com/problems/occurrences-after-bigram/
解题思路
- 将句子按空格分隔开
- 遍历寻找符合条件的就行
代码
|
/** |
|
* @param {string} text |
|
* @param {string} first |
|
* @param {string} second |
|
* @return {string[]} |
|
*/ |
|
var findOcurrences = function(text, first, second) { |
|
let thirdWord = [];//结果 |
|
// 将字符串转为数组 |
|
const textArr = text.split(' '); |
|
for(let i = 0 ;i<textArr.length-2;i++){ |
|
if(textArr[i]===first &&textArr[i+1] === second){ |
|
thirdWord.push(textArr[i+2]) |
|
} |
|
} |
|
return thirdWord; |
|
}; |