[LeetCode] 1078. Occurrences After Bigram

Chobby·2025년 7월 4일
1

LeetCode

목록 보기
459/481

😎풀이

  1. text를 공백을 기준으로 분리
  2. 각 단어를 순회하며 firstsecond가 연달아 나온 후의 단어인지 확인
  3. 2의 조건을 충족한다면 result에 push
  4. 결괏값 반환환
function findOcurrences(text: string, first: string, second: string): string[] {
    const words = text.split(' ')
    const result = []
    for(let i = 2; i < words.length; i++) {
        const firstPrev = words[i - 2]
        const secondPrev = words[i - 1]
        if(firstPrev === first && secondPrev === second) result.push(words[i])
    }
    return result
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글