[LeetCode] 1544. Make The String Great

Chobby·2025년 9월 17일
1

LeetCode

목록 보기
546/582

😎풀이

  1. s를 배열로 변환
  2. 변환된 배열 순회
    2-1. 현재 문자와 이전 문자가 대소문자만 다른 같은 알파벳인지 확인
    2-2. 아니라면, 다음 문자로 이동하여 같은 검사 수행
    2-3. 동일하다면, 현재 문자와 이전 문자를 배열에서 제거 후 처음부터 재순회
  3. 배열을 문자열로 변환하여 Greate String 반환환
function makeGood(s: string): string {
    const splitted = [...s]
    let idx = 1
    while(idx < splitted.length) {
        const prev = splitted[idx - 1]
        const cur = splitted[idx]
        const prevCode = prev.charCodeAt(0)
        const curCode = cur.charCodeAt(0)
        const codeGap = Math.abs(prevCode - curCode)
        if(codeGap !== 32) {
            idx++
            continue
        }
        splitted.splice(idx - 1, 2)
        idx = 1
    }
    return splitted.join('')
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글