Given binary string str of length N. The task is to find the maximum count of consecutive substrings str can be divided into such that all the substrings are balanced i.e. they have an equal number of 0s and 1s. If it is not possible to split str satisfying the conditions then return -1.
Example 1:
Input:
S = "0100110101"
Output: 4
Explanation: 
The required substrings are 01, 0011, 01 and 01.
Example 2:
Input:
S = "0111100010"
Output: 3
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function maxSubStr() which takes a string S and returns an integer as output.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= S.length <= 10^{5}

Difficulty: MEDIUM
Tags: String algorithms

Data structures
Skill types: Data structures
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Starter code:
#Back-end complete function Template for Python 3



class Solution:

    def maxSubStr(self,str):

        #Write your code here





Solutions:
class Solution:

	def maxSubStr(self, input_str):
		count = 0
		balance = 0
		for ch in input_str:
			if ch == '0':
				balance += 1
			elif ch == '1':
				balance -= 1
			if balance == 0:
				count += 1
		if balance != 0:
			return -1
		return count


class Solution:

	def maxSubStr(self, str):
		(c1, c2) = (0, 0)
		count = 0
		n = len(str)
		for (i, s) in enumerate(str):
			if s == '0':
				c1 += 1
			if s == '1':
				c2 += 1
			if c1 == c2:
				count += 1
		if c1 != c2:
			return -1
		return count


class Solution:

	def maxSubStr(self, str):
		n = len(str)
		x = 0
		y = 0
		c = 0
		for i in range(0, n):
			if str[i] == '0':
				x += 1
			else:
				y += 1
			if x == y:
				c += 1
		if x != y:
			return -1
		return c


class Solution:

	def maxSubStr(self, str):
		su = count = total = 0
		for i in str:
			if i == '0':
				total -= 1
			else:
				total += 1
		if total != 0:
			return -1
		else:
			for i in str:
				if i == '0':
					su -= 1
				else:
					su += 1
				if su == 0:
					count += 1
			return count


class Solution:

	def maxSubStr(self, s):
		count0 = 0
		count1 = 0
		ans = 0
		for i in range(len(s)):
			if s[i] == '0':
				count0 += 1
			if s[i] == '1':
				count1 += 1
			if count0 == count1:
				ans += 1
		if count0 != count1:
			return -1
		return ans


class Solution:

	def maxSubStr(self, str):
		count = 0
		count1 = 0
		cnt = 0
		for i in range(len(str)):
			if str[i] == '0':
				count += 1
			else:
				count1 += 1
			if count1 == count:
				cnt += 1
				count1 = 0
				count = 0
		if count or count1:
			return -1
		return cnt


class Solution:

	def maxSubStr(self, string):
		(c0, c1) = (0, 0)
		res = 0
		for i in string:
			if i == '0':
				c0 += 1
			else:
				c1 += 1
			if c0 == c1:
				res += 1
		if c0 != c1:
			return -1
		return res


class Solution:

	def maxSubStr(self, str):
		cnt0 = 0
		cnt1 = 0
		ans = 0
		for i in str:
			if i == '0':
				cnt0 += 1
			else:
				cnt1 += 1
			if cnt0 == cnt1:
				ans += 1
		if cnt0 != cnt1:
			return -1
		return ans


class Solution:

	def maxSubStr(self, str):
		if len(str) % 2 != 0:
			return -1
		elif str.count('1') != str.count('0'):
			return -1
		count_0 = 0
		count_1 = 0
		ans = 0
		for i in str:
			if i == '0':
				count_0 += 1
			else:
				count_1 += 1
			if count_0 == count_1:
				ans += 1
		return ans if ans != 0 else -1


class Solution:

	def maxSubStr(self, str):
		if len(str) % 2 != 0:
			return -1
		zero_cnt = one_cnt = 0
		if str[0] == '0':
			zero_cnt = 1
		else:
			one_cnt = 1
		count = 0
		substr_size = 0
		i = 0
		j = 1
		while i < len(str) and j < len(str):
			if str[j] == '0':
				zero_cnt += 1
			else:
				one_cnt += 1
			j += 1
			if zero_cnt > 0 and one_cnt > 0 and (zero_cnt == one_cnt):
				substr_size += j - i
				zero_cnt = one_cnt = 0
				count += 1
				i = j
		return count if count > 0 and substr_size == len(str) else -1


class Solution:

	def maxSubStr(self, str):
		count = 0
		total = 0
		for x in str:
			if x == '0':
				total += 1
			else:
				total -= 1
			if total == 0:
				count += 1
		return count if total == 0 else -1


class Solution:

	def maxSubStr(self, str):
		count0 = 0
		count1 = 0
		count = 0
		for i in range(0, len(str)):
			if str[i] == '0':
				count0 += 1
			if str[i] == '1':
				count1 += 1
			if count0 == count1:
				count += 1
		if count1 != count0:
			return -1
		return count


class Solution:

	def maxSubStr(self, str):
		n = len(str)
		cnt = 0
		cnt1 = 0
		cnt2 = 0
		for i in range(n):
			if str[i] == '0':
				cnt1 += 1
			else:
				cnt2 += 1
			if cnt1 == cnt2:
				cnt += 1
		if cnt1 != cnt2:
			return -1
		return cnt


class Solution:

	def maxSubStr(self, str):
		if len(str) == 0 or len(str) == 1 or '1' not in str or ('0' not in str):
			return -1
		dic = {}
		count_0 = 0
		count_1 = 0
		total = 0
		for i in range(len(str)):
			if str[i] in dic:
				dic[str[i]] += 1
			else:
				dic[str[i]] = 1
		if dic['1'] != dic['0']:
			return -1
		for k in range(len(str)):
			if str[k] == '0':
				count_0 += 1
			if str[k] == '1':
				count_1 += 1
			if count_0 == count_1:
				total += 1
		return total


class Solution:

	def maxSubStr(self, str):
		count_0 = 0
		count_1 = 0
		count = 0
		for i in str:
			if i == '0':
				count_0 += 1
			else:
				count_1 += 1
			if count_0 == count_1:
				count += 1
		if count_0 != count_1:
			return -1
		return count


class Solution:

	def maxSubStr(self, str):
		n = len(str)
		ans = 0
		i = 0
		(zero, one) = (0, 0)
		while i < n:
			if str[i] == '0':
				zero += 1
			else:
				one += 1
			i += 1
			if zero == one:
				ans += 1
		if zero != one:
			return -1
		return ans


class Solution:

	def maxSubStr(self, str):
		c0 = 0
		c1 = 0
		count = 0
		for i in str:
			if i == '1':
				c1 += 1
			else:
				c0 += 1
			if c1 == c0:
				count += 1
		if count > 0 and c0 == c1:
			return count
		return -1


class Solution:

	def maxSubStr(self, str):
		k = j = r = 0
		for i in str:
			if i == '0':
				k += 1
			else:
				j += 1
			if k == j:
				r += 1
				k = j = 0
		if k != 0:
			return -1
		if j != 0:
			return -1
		if r == 0:
			return -1
		return r


class Solution:

	def maxSubStr(self, str):
		count_0 = 0
		count_1 = 0
		ans = 0
		for i in range(len(s)):
			if s[i] == '0':
				count_0 += 1
			else:
				count_1 += 1
			if count_0 == count_1:
				ans += 1
			elif i == len(s) - 1:
				return -1
		if ans == 0:
			return -1
		return ans


class Solution:

	def maxSubStr(self, s):
		count = 0
		z_count = 0
		o_count = 0
		for i in s:
			if i == '0':
				z_count += 1
			else:
				o_count += 1
			if z_count == o_count:
				z_count = 0
				o_count = 0
				count += 1
		if z_count == o_count:
			return count
		else:
			return -1


class Solution:

	def maxSubStr(self, s):
		if len(s) == 1:
			return -1
		if s.count('0') != s.count('1'):
			return -1
		curr = s[0]
		curr_c = 0
		count = 0
		i = 0
		while i < len(s):
			if s[i] == curr:
				curr_c += 1
			else:
				while i < len(s) and s[i] != curr and (curr_c != 0):
					curr_c -= 1
					i += 1
				if curr_c == 0:
					count += 1
				if i < len(s):
					curr = s[i]
				i -= 1
			i += 1
		return count


class Solution:

	def maxSubStr(self, s):
		if s.count('0') != s.count('1'):
			return -1
		count_0 = 0
		count_1 = 0
		ans = 0
		for elm in s:
			if elm == '0':
				count_0 += 1
			else:
				count_1 += 1
			if count_0 == count_1:
				ans += 1
		return ans


class Solution:

	def maxSubStr(self, str):
		c = c1 = c2 = 0
		for i in str:
			if i == '0':
				c1 += 1
			else:
				c2 += 1
			if c1 == c2:
				c += 1
		if c1 != c2:
			return -1
		return c


class Solution:

	def maxSubStr(self, str):
		p = 0
		res = 0
		for i in str:
			if i == '1':
				p -= 1
			else:
				p += 1
			if p == 0:
				res += 1
		if p != 0:
			return -1
		return res


class Solution:

	def maxSubStr(self, stre):
		n = len(stre)
		(vote, ans) = (0, 0)
		for i in stre:
			if i == '0':
				vote += 1
			else:
				vote -= 1
			if vote == 0:
				ans += 1
		if vote == 0:
			return ans
		return -1


class Solution:

	def maxSubStr(self, str):
		count0 = 0
		count1 = 0
		n = len(str)
		cnt = 0
		for i in range(n):
			if str[i] == '0':
				count0 += 1
			else:
				count1 += 1
			if count0 == count1:
				cnt += 1
		if count0 != count1:
			return -1
		return cnt


class Solution:

	def maxSubStr(self, str):
		a = 0
		b = 0
		c = 0
		i = 0
		while i < len(str):
			if str[i] == '0':
				a = a + 1
			else:
				b = b + 1
			if a == b:
				a = 0
				b = 0
				c = c + 1
			i = i + 1
		if a == 0 and b == 0:
			return c
		return -1


class Solution:

	def maxSubStr(self, str):
		ca = 0
		count = 0
		for i in str:
			if i == '0':
				ca += 1
			else:
				ca -= 1
			if ca == 0:
				count += 1
		if ca == 0:
			return count
		else:
			return -1


class Solution:

	def maxSubStr(self, str):
		i = 0
		o = 0
		res = 0
		for k in range(len(str) - 1):
			if str[k] == '0':
				o = o + 1
				if o == i:
					res = res + 1
					i = 0
					o = 0
			else:
				i = i + 1
				if o == i:
					res = res + 1
					i = 0
					o = 0
		k = len(str) - 1
		if str[k] == '0':
			o = o + 1
			if o == i:
				res = res + 1
			else:
				return -1
		else:
			i = i + 1
			if o == i:
				res = res + 1
			else:
				return -1
		if res == 0:
			return -1
		else:
			return res


class Solution:

	def maxSubStr(self, str):
		count_zero = 0
		count_one = 0
		count = 0
		for i in range(len(str)):
			if str[i] == '0':
				count_zero += 1
			else:
				count_one += 1
			if count_zero == count_one:
				count += 1
		if count_zero != count_one:
			return -1
		return count


class Solution:

	def maxSubStr(self, str):
		n = len(str)
		(cz, count) = (0, 0)
		for i in str:
			if i == '0':
				cz += 1
			else:
				cz -= 1
			if cz == 0:
				count += 1
		if cz == 0:
			return count
		return -1


class Solution:

	def maxSubStr(self, str):
		i = 0
		s = 0
		c1 = 0
		c2 = 0
		while i < len(str):
			if str[i] == '0':
				c1 += 1
			else:
				c2 += 1
			if c1 == c2:
				s += 1
				c1 = 0
				c2 = 0
			i += 1
		if c1 == c2:
			return s
		else:
			return -1


class Solution:

	def maxSubStr(self, str):
		count = 0
		c1 = 0
		c0 = 0
		i = 0
		u = ''
		while i < len(s):
			u += s[i]
			if s[i] == '0':
				c0 += 1
			else:
				c1 += 1
			if c1 == c0:
				count += 1
				u = ''
				c1 = 0
				c0 = 0
			if i == len(s) - 1:
				if u != '':
					return -1
			i += 1
		return count


class Solution:

	def maxSubStr(self, str):
		count = 0
		zeroes = 0
		ones = 0
		for i in str:
			if i == '0':
				zeroes += 1
			else:
				ones += 1
			if zeroes == ones:
				count += 1
				zeroes = 0
				ones = 0
		if ones != zeroes:
			return -1
		if count == 0:
			return -1
		return count


class Solution:

	def maxSubStr(self, str):
		cz = 0
		co = 0
		out = 0
		for i in str:
			if i == '0':
				cz += 1
			else:
				co += 1
			if cz == co:
				out += 1
		if co != cz:
			return -1
		return out


class Solution:

	def maxSubStr(self, str):
		c0 = 0
		c1 = 0
		c = 0
		for i in range(len(str)):
			if str[i] == '0':
				c0 += 1
			elif str[i] == '1':
				c1 += 1
			if c0 == c1:
				c += 1
		if c0 != c1:
			return -1
		return c


class Solution:

	def maxSubStr(self, str):
		ans = 0
		res = 0
		lis = list(str)
		for i in range(0, len(lis)):
			if lis[i] == '1':
				ans = ans + 1
			if lis[i] == '0':
				ans = ans - 1
			if ans == 0:
				res += 1
				ans = 0
		if ans != 0:
			return -1
		return res


class Solution:

	def maxSubStr(self, str):
		zero = 0
		one = 0
		c = 0
		stack = []
		temp = ''
		for i in range(len(str)):
			if str[i] == '0':
				if stack != [] and stack[-1] == '1':
					x = stack.pop()
				else:
					stack.append('0')
			elif str[i] == '1':
				if stack != [] and stack[-1] == '0':
					x = stack.pop()
				else:
					stack.append('1')
			if stack == []:
				c += 1
		if stack != []:
			return -1
		return c


class Solution:

	def maxSubStr(self, str):
		mapping = {'1': 0, '0': 0}
		count = 0
		for element in str:
			mapping[element] += 1
			if mapping['0'] == mapping['1']:
				mapping['0'] = 0
				mapping['1'] = 0
				count += 1
		if mapping['0'] == mapping['1'] == 0:
			return count
		return -1


class Solution:

	def maxSubStr(self, str):
		d1 = {}
		for i in str:
			d1[i] = 1 + d1.get(i, 0)
		if len(set(d1.values())) > 1:
			return -1
		d = {'0': 0, '1': 0}
		i = 0
		j = 0
		c = -1
		f = 0
		while j < len(str):
			d[str[j]] = d[str[j]] + 1
			if d['0'] != d['1']:
				j = j + 1
			else:
				c = c + 1
				f = 1
				d = {'0': 0, '1': 0}
				i = j + 1
				j = i
		if f == 1:
			c = c + 1
			return c
		else:
			return c


class Solution:

	def maxSubStr(self, str):
		i = 0
		cnt0 = 0
		cnt1 = 0
		cnt = 0
		while i < len(str):
			if str[i] == '0':
				cnt0 += 1
			else:
				cnt1 += 1
			if cnt0 == cnt1:
				cnt += 1
			i += 1
		if cnt0 == cnt1:
			return cnt
		else:
			return -1


class Solution:

	def maxSubStr(self, s):
		i = 0
		cnt0 = 0
		cnt1 = 0
		ans = 0
		while i < len(s):
			if s[i] == '0':
				cnt0 += 1
			else:
				cnt1 += 1
			if cnt0 == cnt1:
				ans += 1
			i += 1
		if ans == 0 or cnt0 != cnt1:
			return -1
		return ans


class Solution:

	def maxSubStr(self, str):
		check = ''
		count = 0
		val = 0
		for i in str:
			if count == 0:
				check = i
				count += 1
			elif check == i:
				count += 1
			else:
				count -= 1
			if count == 0:
				val += 1
		if count != 0:
			return -1
		return val


class Solution:

	def maxSubStr(self, str):
		if str.count('1') != str.count('0'):
			return -1
		count = 0
		ones = 0
		zeros = 0
		for i in range(len(str)):
			if ones == zeros:
				count += 1
				ones = 0
				zeros = 0
			if str[i] == '1':
				ones += 1
			else:
				zeros += 1
		return count


class Solution:

	def maxSubStr(self, str):
		count = 0
		zero = 0
		one = 0
		for i in str:
			if i == '0':
				zero += 1
			elif i == '1':
				one += 1
			if zero == one:
				count += 1
		if zero == one:
			return count
		else:
			return -1


class Solution:

	def maxSubStr(self, str):
		c = 0
		zero = 0
		one = 0
		for i in range(len(str)):
			if str[i] == '0':
				zero = zero + 1
			else:
				one = one + 1
			if zero == one:
				c = c + 1
		if zero != one:
			return -1
		return c


class Solution:

	def maxSubStr(self, str):
		count0 = 0
		count1 = 0
		ans = 0
		for i in str:
			if i == '0':
				count0 += 1
			else:
				count1 += 1
			if count1 == count0:
				ans += 1
		if count1 != count0:
			return -1
		return ans


class Solution:

	def maxSubStr(self, str):
		c = 0
		zc = 0
		oc = 0
		for i in s:
			if i == '0':
				zc += 1
			else:
				oc += 1
			if zc == oc:
				c += 1
				zc = 0
				oc = 0
		if zc >= 1 or oc >= 1:
			return -1
		else:
			return c


class Solution:

	def maxSubStr(self, str):
		countZero = 0
		countOne = 0
		res = 0
		for i in range(len(str)):
			if str[i] == '0':
				countZero += 1
			else:
				countOne += 1
			if countZero == countOne:
				res += 1
				countZero = 0
				countOne = 0
		if countZero > 0 or countOne > 0 or countZero != countOne:
			return -1
		return res


class Solution:

	def maxSubStr(self, str):
		count_0 = 0
		count_1 = 0
		count = 0
		for i in range(len(str)):
			if str[i] == '0':
				count_0 += 1
			else:
				count_1 += 1
			if count_0 == count_1:
				count += 1
		if count_0 != count_1:
			return -1
		elif count_0 == 0 and count_1 == 0:
			return -1
		return count


class Solution:

	def maxSubStr(self, str):
		z = 0
		ones = 0
		ans = 0
		for i in str:
			if i == '0':
				z += 1
			else:
				ones += 1
			if z == ones:
				ans += 1
				(z, ones) = (0, 0)
		return ans if z == ones else -1


class Solution:

	def maxSubStr(self, str):
		(one, zero, ans) = (0, 0, 0)
		for i in str:
			if i == '0':
				zero += 1
			else:
				one += 1
			if zero == one:
				ans += 1
				(one, zero) = (0, 0)
		if ans == 0 or one != zero:
			return -1
		else:
			return ans


from collections import Counter

def func(s, temp, ans):
	if not s:
		ans[0] = max(ans[0], len(temp))
		return
	for i in range(2, len(s) + 1):
		t = s[:i]
		a = Counter(t)
		if a.get('0', 0) == a.get('1', 0):
			func(s[i:], temp + [t], ans)

class Solution:

	def maxSubStr(self, s):
		l = 0
		count = 0
		c0 = 0
		c1 = 0
		for i in range(len(s)):
			if s[i] == '0':
				c0 += 1
			else:
				c1 += 1
			if c0 == c1:
				c0 = 0
				c1 = 0
				count += 1
		if c0 != c1:
			return -1
		return count


class Solution:

	def maxSubStr(self, str):
		counter = 0
		count = 0
		d = {'0': 0, '1': 0}
		for i in str:
			d[i] += 1
			if i == '0':
				counter += 1
			else:
				counter -= 1
			if counter == 0:
				count += 1
		return count if d['0'] == d['1'] else -1


class Solution:

	def maxSubStr(self, str):
		count0 = 0
		count1 = 0
		ans = 0
		i = 0
		while i < len(str):
			if str[i] == '0':
				count0 += 1
			else:
				count1 += 1
			if count0 == count1:
				ans += 1
			i += 1
		if count0 == count1:
			return ans
		return -1


class Solution:

	def maxSubStr(self, s):
		count = 0
		summa = 0
		for item in s:
			if item == '1':
				summa += 1
			else:
				summa -= 1
			count += summa == 0
		return count if summa == 0 else -1


class Solution:

	def maxSubStr(self, str1):
		(count0, count1, res) = (0, 0, 0)
		for i in str1:
			if i == '0':
				count0 += 1
			else:
				count1 += 1
			if count0 == count1:
				(count0, count1, res) = (0, 0, res + 1)
		if count0 > 0 or count1 > 0 or count0 != count1:
			return -1
		return res


class Solution:

	def maxSubStr(self, str):
		(zero, one) = (0, 0)
		ans = 0
		for each in str:
			if each == '0':
				zero += 1
			if each == '1':
				one += 1
			if zero == one:
				(zero, one) = (0, 0)
				ans += 1
		if zero == one:
			return ans
		else:
			return -1


class Solution:

	def maxSubStr(self, value):
		n = len(value)
		first_element = value[0]
		last_element = first_element
		count = 0
		count = count + self.operation(last_element, first_element)
		pair = 0
		for i in range(1, n):
			element = value[i]
			count = count + self.operation(element, first_element)
			if count == 0:
				pair = pair + 1
		if pair == 0 or count != 0:
			return -1
		else:
			return pair

	def operation(self, element, first_element):
		if element == first_element:
			return 1
		else:
			return -1


class Solution:

	def maxSubStr(self, str):
		c0 = 0
		c1 = 0
		cnt = 0
		for i in range(len(str)):
			if str[i] == '0':
				c0 += 1
			else:
				c1 += 1
			if c0 == c1:
				cnt += 1
		if c0 != c1:
			return -1
		return cnt


class Solution:

	def maxSubStr(self, str):
		count = 0
		zero = 0
		ones = 0
		for i in range(len(str)):
			if str[i] == '0':
				zero += 1
			elif str[i] == '1':
				ones += 1
			if zero == ones:
				count += 1
		if zero != ones:
			return -1
		return count


class Solution:

	def maxSubStr(self, str):
		count0 = 0
		count1 = 0
		count = 0
		for (i, j) in enumerate(str):
			if j == '0':
				count0 += 1
			else:
				count1 += 1
			if count0 == count1:
				count += 1
		if count0 != count1:
			return -1
		return count


class Solution:

	def maxSubStr(self, str):
		c1 = 0
		c2 = 0
		c3 = 0
		for i in range(len(str)):
			if str[i] == '0':
				c1 += 1
			else:
				c2 += 1
			if c1 == c2:
				c3 += 1
		if c1 != c2:
			return -1
		return c3


class Solution:

	def maxSubStr(self, str):
		count = 0
		arr = []
		sum = 0
		for i in range(0, len(str)):
			if str[i] == '1':
				arr.append(1)
			else:
				arr.append(-1)
		for i in range(0, len(str)):
			sum += arr[i]
			if sum == 0:
				count += 1
		if sum != 0:
			return -1
		return count


class Solution:

	def maxSubStr(self, str1):
		o = 0
		z = 0
		c = 0
		res = []
		for i in str1:
			if i == '0':
				z = z + 1
			if i == '1':
				o = o + 1
			if z == o:
				res.append(str1[:z + o])
				str1 = str1[z + o:]
				z = 0
				o = 0
		if z > 0 or o > 0:
			return -1
		else:
			return len(res)


class Solution:

	def maxSubStr(self, str):
		m = 0
		n = 0
		c = 0
		if str.count('1') != str.count('0'):
			return -1
		else:
			for i in range(len(str)):
				if str[i] == '0':
					m += 1
				elif str[i] == '1':
					n += 1
				if m == n:
					m = 0
					n = 0
					c += 1
			return c


class Solution:

	def maxSubStr(self, s):
		c1 = c2 = 0
		ans = 0
		for i in s:
			if i == '0':
				c1 += 1
			else:
				c2 += 1
			if c1 == c2:
				ans += 1
		if c1 != c2:
			return -1
		return ans


class Solution:

	def maxSubStr(self, str):
		(x, y, ans) = (0, 0, 0)
		for i in range(len(str)):
			if str[i] == '0':
				x = x + 1
			elif str[i] == '1':
				y = y + 1
			if x == y:
				ans = ans + 1
		if x != y:
			return -1
		return ans


class Solution:

	def maxSubStr(self, str):
		i = 0
		j = 0
		m_i = 0
		m_j = 0
		count = 0
		for k in range(len(str)):
			if str[k] == '0':
				i += 1
				m_i += 1
			else:
				j += 1
				m_j += 1
			if i == j:
				count += 1
				i = 0
				j = 0
		if m_i != m_j:
			return -1
		else:
			return count


class Solution:

	def maxSubStr(self, str):
		c = 0
		z = 0
		o = 0
		for i in str:
			if i == '0':
				z += 1
			else:
				o += 1
			if z == o:
				z = 0
				o = 0
				c += 1
		if z != 0 or o != 0:
			return -1
		if c == 0:
			return -1
		return c