There was this question on hackerrank to find the minimum coefficient of a binary string which can be found by following two ways :

count the number of 0’s between two consecutive 1’s, lets say a.

count the number of 1’s between two consecutive 0’s, lets say b.
so the coefficient results to be a+b.
We need to minimize the coefficient by the operation that is given in the below question link.
Question : Programming Problems and Competitions :: HackerRank
My solution : Programming Problems and Competitions :: HackerRank
I need suggestions for an optimal code and any other way to approach this problem is appreciated.