# LeetCode: Minimum Changes To Make Alternating Binary String Solution

## Approach

Create 2 possible output

s1
,
s2

Calculate the number of steps from

s
to each of those

Find min between 2 of them

## Implementation

`.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 * @param {string} s3 * @return {number}4 */5var minOperations = function (s) {6  const N = s.length7  const chars = s.split("").map(Number)8  const s1 = Array.from({ length: N }, (_, i) => (i % 2 === 0 ? 1 : 0))9  const s2 = Array.from({ length: N }, (_, i) => (i % 2 === 0 ? 0 : 1))10  return Math.min.apply(11    null,12    chars.reduce(13      (acc, el, i) => [14        acc[0] + (el !== s1[i] ? 1 : 0),15        acc[1] + (el !== s2[i] ? 1 : 0),16      ],17      [0, 0]18    )19  )20}`

leetcode

array

greedy

## Next Post

LeetCode: Count Number Of Homogenous Substrings

Feb 14, 2021

Search Posts