[LeetCode] 3274. Check if Two Chessboard Squares Have the Same Color

Chobby·2026년 1월 6일

LeetCode

목록 보기
884/986

😎풀이

  1. a부터 h까지의 알파벳 1 ~ 8의 정수로 맵핑
  2. 각 좌표에서 알파벳과 정수를 분리
  3. 정수화 했을 때, 해당 좌표가 홀수라면 흰 칸, 짝수라면 검정 칸이 됨
  4. 그에 따른 두 좌표의 홀짝여부 동등비교
function checkTwoChessboards(coordinate1: string, coordinate2: string): boolean {
    const map = new Map<string, number>()
    for(let i = 1; i <= 8; i++) {
        map.set(String.fromCharCode(i + 96), i)
    }
    const [alpha1, int1] = [...coordinate1]
    const [alpha2, int2] = [...coordinate2]
    return isWhite(map.get(alpha1), Number(int1)) === isWhite(map.get(alpha2), Number(int2))
};

function isWhite(num1: number, num2: number) {
    const sum = num1 + num2
    return (sum & 1) === 1
}
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글