😎풀이

  1. 최대 반복 수 정의
  2. sequence 순회
    2-1. 현재 인덱스에서 시작해 word로 반복 가능한 수 확인
    2-2. 최대 반복 수 갱신
  3. 최대 반복 수 반환
function maxRepeating(sequence: string, word: string): number {
    let maxRepeat = 0
    function repeat(startPos: number) {
        let count = 0
        for(let i = startPos; i < sequence.length; i++) {
            const curSeq = sequence[i]
            const curWordIdx = (i - startPos) % word.length
            const curWord = word[curWordIdx]
            if(curSeq !== curWord) break
            if(curWordIdx === word.length - 1) count++
        }
        return count
    }
    for(let i = 0; i < sequence.length; i++) {
        const curRepeat = repeat(i)
        maxRepeat = Math.max(maxRepeat, curRepeat)
    }
    return maxRepeat
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글