Need suggestions for an optimal way to get all the test cases AC!

There was this question on hackerrank to find the minimum co-efficient 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 co-efficient results to be a+b.
    We need to minimize the co-efficient 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.