# LeetCode: Count Binary Substrings Solution

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}/*2  group and count contiguous 0s/1s3    "00110011"4    => [2, 2, 2, 2]5
6  sum of the minimum adjacent pairs7    [2, 2, 2, 2]8    => 2 + 2 + 2 = 69*/10
11/**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  }25
26  // 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  }31
32  return res33}```

leetcode

string

## Next Post

LeetCode: Power Of Three

Apr 27, 2021

Search Posts