LeetCode: Count Binary Substrings Solution
1/*2 group and count contiguous 0s/1s3 "00110011"4 => [2, 2, 2, 2]56 sum of the minimum adjacent pairs7 [2, 2, 2, 2]8 => 2 + 2 + 2 = 69*/1011/**12 * @param {string} s13 * @return {number}14 */15var countBinarySubstrings = function (s) {16 // group and count contiguous 0s/1s17 const groups = [1]18 for (let i = 1; i < s.length; i++) {19 if (s[i] === s[i - 1]) {20 groups[groups.length - 1]++21 } else {22 groups.push(1)23 }24 }2526 // sum of the minimum adjacent27 let res = 028 for (let i = 0; i < groups.length - 1; i++) {29 res += Math.min(groups[i], groups[i + 1])30 }3132 return res33}
Comments
Loading comments...
Tags
leetcode
string
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
LeetCode: Power Of Three
Apr 27, 2021
Previous Post
LeetCode: Brick Wall
Apr 22, 2021