[leetcode, JS] 2609. Find the Longest Balanced Substring of a Binary String

mxxn·2024년 6월 5일
0

leetcode

목록 보기
172/198

문제

문제 링크 : Find the Longest Balanced Substring of a Binary String

풀이

/**
 * @param {string} s
 * @return {number}
 */
var findTheLongestBalancedSubstring = function(s) {
    if(!s.includes('0')) {
        return 0
    }else {
        let result = 0
        let substr = ''
        while(true) {
            substr = '0' + substr + '1'
            if(s.includes(substr)) {
                result = substr.length
            }else {
                return result
            }
        }
    }
};
  1. s에 0이 없다면 우선 0 return
  2. 그 외에 경우엔, '01'부터 시작해서 s에 포함된 substr 구해서 result 값 return
  • Runtime 84 ms, Memory 53.60 MB
profile
내일도 글쓰기

0개의 댓글