[leetcode, JS] 1758. Minimum Changes To Make Alternating Binary String

mxxn·2023년 11월 8일
0

leetcode

목록 보기
114/198

문제

문제 링크 : Minimum Changes To Make Alternating Binary String

풀이

/**
 * @param {string} s
 * @return {number}
 */
var minOperations = function(s) {
    let counter1=0;
    let counter2=0;
    for(let i=0;i<s.length;i++){
        if(i%2===0){
            if(s[i]==="0"){
                counter1++;
            }
            if(s[i]==="1"){
                counter2++;
            }
        }
        if(i%2===1){
            if(s[i]==="1"){
                counter1++;
            }
            if(s[i]==="0"){
                counter2++;
            }
        }
    }
    return Math.min(counter1,counter2);
};
  1. 주어진 문자열 s가 0으로 시작했을 때 바뀌어야 하는 카운트와 1로 시작했을 때 바뀌어야 할 카운트를 계산하는 방식
  • Runtime 46ms, Memory 42.64MB
profile
내일도 글쓰기

0개의 댓글