[leetcode, JS] 1078. Occurrences After Bigram

mxxn·2023년 9월 5일
0

leetcode

목록 보기
64/198

문제

문제 링크 : Occurrences After Bigram

풀이

/**
 * @param {string} text
 * @param {string} first
 * @param {string} second
 * @return {string[]}
 */
var findOcurrences = function(text, first, second) {
    const textArr = text.split(' ')
    let result = []
    for(let i=2; i<textArr.length; i++) {
        if(textArr[i-2] === first && textArr[i-1] === second) result.push(textArr[i])
    }

    return result;
};
  1. text를 ' '로 split하고
  2. 2번째 index부터 for문을 돌기 시작하여, first와 second가 바로 앞에 위치한 값을 result에 push
  • Runtime 37 ms, Memory 41.7 MB
profile
내일도 글쓰기

0개의 댓글