A binary string is called a self-destructing string if it can reduced to an empty string by performing the following operation some number of times (possibly zero): Choose a valid integer $i$ such that the $i$-th character of the current string is different from the $i+1$-th character, and remove these two characters from the string.
You are given a binary string $s$. Your task is to convert $s$ to a self-destructing string. To do that, you may perform the following operation any number of times (possibly zero): Choose an integer $i$ ($1 \le i \le |s|-1$) such that the $i$-th character of $s$ is different from the $i+1$-th character, and invert one of these characters (inverting a character means changing '0' to '1' or '1' to '0', e.g. the string "01" can be changed to "00").
Find the smallest number of operations required to convert $s$ to a self-destructing string or determine that it is impossible.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains a single string $s$.

-----Output-----
For each test case, print a single line containing one integer ― the smallest required number of operations or $-1$ if it is impossible to convert $s$ to a self-destructing string.

-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le |s| \le 10^6$
- $s$ contains only characters '0' and '1'
- the sum of $|s|$ over all test cases does not exceed $10^6$

-----Example Input-----
3
001101
1101
110

-----Example Output-----
0
1
-1

-----Explanation-----
Example case 1: We already have a self-destructing string because "001101" can be converted to "0101", to "01" and finally to an empty string.
Example case 2: We can choose $i=3$ (since $s_3 \neq s_4$) and then invert $s_4$ to obtain "1100", which is a self-destructing string.
Example case 3: It can be shown that "110" cannot be converted to a self-destructing string.

Difficulty: MEDIUM_HARD
Tags: String algorithms

Data structures
Skill types: Data structures

Starter code:

Solutions:
t = int(input())
for i in range(0, t):
	s = input()
	if len(s) % 2 != 0:
		print('-1')
	elif len(s) % 2 == 0:
		l = s.count('0')
		m = s.count('1')
		if l == 0 or m == 0:
			print('-1')
		else:
			print(abs(l - m) // 2)


t = int(input())
for i in range(t):
	s = input()
	k = len(s)
	if k % 2 != 0:
		print('-1')
	else:
		a = s.count('0')
		b = s.count('1')
		if a == b:
			print('0')
		elif a == 0 or b == 0:
			print('-1')
		else:
			print(abs(a - b) // 2)


n = int(input())
for i in range(n):
	a = input()
	if len(a) % 2 != 0:
		print('-1')
	elif len(a) % 2 == 0:
		l = 0
		m = 0
		l = a.count('0')
		m = a.count('1')
		if l == 0 or m == 0:
			print('-1')
		else:
			print(abs(l - m) // 2)


t = int(input())
while t > 0:
	s = input()
	if len(s) % 2 != 0:
		print(-1)
		t -= 1
		continue
	ones = 0
	zeros = 0
	for i in s:
		if i == '1':
			ones += 1
		else:
			zeros += 1
	if zeros == len(s) or ones == len(s):
		print(-1)
		t -= 1
		continue
	print(abs(ones - zeros) // 2)
	t -= 1


t = int(input())
for i in range(t):
	s = input()
	n = len(s)
	if n % 2 == 1:
		print(-1)
	else:
		o = s.count('1')
		z = s.count('0')
		if o == n or z == n:
			print(-1)
		else:
			print(n // 2 - min(o, z))


for _ in range(int(input())):
	s = input()
	if s.count('0') == 0 or s.count('0') == len(s):
		print(-1)
	else:
		print(abs(s.count('0') - s.count('1')) // 2 if len(s) % 2 == 0 else -1)


def soln(s: str) -> int:
	n = len(s)
	ones = s.count('1')
	zeros = s.count('0')
	diff = abs(ones - zeros)
	if diff & 1:
		return -1
	return -1 if ones != zeros and (ones == 0 or zeros == 0) else diff // 2
for _ in range(int(input())):
	s = input()
	print(soln(s))


for _ in range(int(input())):
	st = input()
	l = len(st)
	if l % 2 == 1:
		print(-1)
	else:
		z = st.count('0')
		o = st.count('1')
		diff = int(abs(o - z))
		if o == 0 or z == 0:
			print(-1)
		else:
			print(diff // 2)


t = int(input())
while t > 0:
	s = input()
	n = len(s)
	x = s.count('1')
	if n % 2 == 1 or x == 0 or x == n:
		print(-1)
	else:
		print(abs(n // 2 - x))
	t -= 1


for _ in range(int(input())):
	s = list(input())
	n = len(s)
	if n % 2 != 0:
		print('-1')
	else:
		i = 0
		count0 = s.count('0')
		count1 = s.count('1')
		if count0 == 0:
			print('-1')
		elif count1 == 0:
			print('-1')
		else:
			ans = abs(count1 - count0) // 2
			print(ans)


def solve(b, n):
	x = 0
	y = 0
	for i in range(n):
		if b[i] == '1':
			x += 1
		else:
			y += 1
	if x == n or y == n:
		return -1
	return abs(x - y) // 2
for tests in range(int(input())):
	b = input()
	n = len(b)
	if n % 2 == 1:
		print(-1)
	else:
		print(solve(b, n))


for _ in range(int(input())):
	s = input()
	n = len(s)
	zero = s.count('0')
	ones = s.count('1')
	if zero == n or ones == n:
		print(-1)
		continue
	if n & 1:
		print(-1)
	elif zero == ones:
		print(0)
	else:
		rem = n // 2
		rem = rem - min(zero, ones)
		print(rem)


try:
	t = int(input())
	for k in range(t):
		s = input()
		ans = 0
		if len(s) % 2 != 0 or not '0' in s or (not '1' in s):
			ans = -1
		else:
			zeros = 0
			ones = 0
			for i in s:
				if i == '1':
					ones += 1
				else:
					zeros += 1
			ans = abs(zeros - ones) // 2
		print(ans)
except:
	pass


t = int(input())
for i in range(1, t + 1):
	st = input()
	a = st.count('0', 0, len(st))
	b = st.count('1', 0, len(st))
	if len(st) % 2 == 1 or a == 0 or b == 0:
		ans = -1
	else:
		ans = abs(a - b) // 2
	print(ans)


t = int(input())
for i in range(t):
	s = input()
	if len(s) % 2 != 0:
		print('-1')
	else:
		one = s.count('1')
		zero = s.count('0')
		if not zero or not one:
			print('-1')
		else:
			print(abs(zero - one) // 2)


for _ in range(int(input())):
	st = input()
	ln = len(st)
	if ln % 2 == 1:
		print(-1)
	else:
		cnt0 = st.count('0')
		cnt1 = st.count('1')
		df = int(abs(cnt1 - cnt0))
		if cnt1 == 0 or cnt0 == 0:
			print(-1)
		else:
			print(df // 2)


for _ in range(int(input())):
	s = input()
	l = len(s)
	if l % 2 == 1:
		print(-1)
	else:
		c1 = s.count('1')
		c0 = l - c1
		if c0 == 0 or c1 == 0:
			print(-1)
		elif c1 == c0:
			print(0)
		else:
			print(abs(c1 - c0) // 2)


t = int(input())
for _ in range(t):
	s = input()
	if len(s) % 2 != 0:
		print('-1')
	else:
		a = s.count('1')
		b = s.count('0')
		if a == 0:
			print('-1')
		elif b == 0:
			print('-1')
		else:
			c = abs(a - b) // 2
			print(c)


for _ in range(int(input())):
	n = list(input())
	if len(n) % 2 == 1:
		print(-1)
	else:
		s1 = n.count('0')
		s2 = n.count('1')
		if s1 > 0 and s2 > 0:
			print(abs(s1 - s2) // 2)
		else:
			print(-1)


t = int(input())
for _ in range(t):
	s = list(input())
	n = len(s)
	if n % 2 == 1:
		print(-1)
	else:
		i = 0
		k1 = s.count('1')
		k2 = s.count('0')
		if k1 == k2:
			print(0)
		elif k1 == 0 or k2 == 0:
			print(-1)
		else:
			kmin = min(k1, k2)
			required = n // 2
			print(required - kmin)


for _ in range(int(input())):
	s = input()
	lisS = list(s)
	if len(set(lisS)) == 1 or len(s) % 2 != 0:
		print(-1)
	else:
		mid = len(s) // 2
		numZeros = 0
		for i in range(len(s)):
			if s[i] == '0':
				numZeros += 1
		print(abs(mid - numZeros))


for u in range(int(input())):
	s = input()
	cnt0 = s.count('0')
	cnt1 = s.count('1')
	if len(s) % 2 != 0 or cnt0 == len(s) or cnt1 == len(s):
		print(-1)
	else:
		d = abs(cnt1 - cnt0)
		if d == 0:
			print(0)
		else:
			print(d // 2)


tests = int(input())
for i in range(tests):
	s = input()
	if len(s) % 2 == 1:
		print(-1)
	else:
		d = {'0': 0, '1': 0}
		for i in s:
			if i == '0':
				d['0'] += 1
			else:
				d['1'] += 1
		if d['0'] == 0 or d['1'] == 0:
			print(-1)
		else:
			print(abs(d['1'] - d['0']) // 2)


t = int(input())
for i in range(t):
	s = input()
	zeroes = s.count('0')
	ones = s.count('1')
	if len(s) % 2 == 1 or zeroes == 0 or ones == 0:
		ans = -1
	else:
		ans = abs(zeroes - ones) // 2
	print(ans)


a = int(input())
for x in range(a):
	b = input()
	c = b.count('1')
	d = b.count('0')
	if c == len(b) or d == len(b):
		print(-1)
	elif len(b) & 1 == 1:
		print(-1)
	else:
		print(int(len(b) / 2) - min(c, d))


t = int(input())
for t1 in range(t):
	n = input()
	if len(n) % 2:
		print('-1')
	else:
		z = 0
		for i in n:
			if i == '0':
				z += 1
		if z == 0 or z == len(n):
			print('-1')
		else:
			print(abs(len(n) // 2 - z))


n = int(input())
for i in range(n):
	s = input()
	if len(s) % 2 != 0:
		print('-1')
	else:
		flag = 0
		l = list(s)
		zero = l.count('0')
		one = l.count('1')
		if zero == one:
			print('0')
		elif zero == 0 or one == 0:
			print('-1')
		else:
			print(abs(zero - one) // 2)


for case in range(int(input())):
	s = input()
	o = s.count('1')
	n = len(s)
	if n % 2 == 1 or abs(n // 2 - o) == n // 2:
		print(-1)
	else:
		print(abs(n // 2 - o))


a = int(input())
for i in range(a):
	b = list(map(int, str(input())))
	if len(b) % 2 != 0:
		print(-1)
	elif sum(b) == len(b) or sum(b) == 0:
		print(-1)
	else:
		print(abs(len(b) // 2 - sum(b)))


for _ in range(int(input())):
	s = str(input())
	if len(s) & 1 == 1:
		print(-1)
	else:
		z = 0
		o = 0
		for i in range(len(s)):
			if s[i] == '0':
				z += 1
			else:
				o += 1
		if z == o:
			print(0)
		elif (z or o) != len(s):
			print(int(abs(z - o) / 2))
		else:
			print(-1)


t = int(input())
for i in range(t):
	s = input()
	if len(s) % 2 != 0:
		print('-1')
	elif len(s) % 2 == 0:
		x = 0
		y = 0
		x = s.count('0')
		y = s.count('1')
		if x == 0 or y == 0:
			print('-1')
		else:
			print(int(abs(y - x) / 2))


for _ in range(int(input())):
	s = input()
	n = len(s)
	if len(s) % 2 != 0:
		print('-1')
	elif len(s) % 2 == 0:
		zeros = s.count('0')
		ones = s.count('1')
		if zeros == 0 or ones == 0:
			print('-1')
		else:
			print(abs(zeros - ones) // 2)


def func(s):
	c1 = 0
	c2 = 0
	l = len(s)
	if l % 2 != 0:
		return '-1'
	for i in s:
		if i == '1':
			c1 += 1
		elif i == '0':
			c2 += 1
	if c1 == 0 or c2 == 0:
		return -1
	else:
		return abs(c1 - c2) // 2
n = int(input())
for i in range(n):
	s = input()
	print(func(s))


for i in range(int(input())):
	s = input()
	n = len(s)
	n1 = s.count('1')
	n0 = s.count('0')
	if n1 == n or n0 == n:
		print(-1)
		continue
	if n & 1:
		print(-1)
	elif n1 == n0:
		print(0)
	else:
		n2 = n1 - n0
		if n2 < 0:
			n2 *= -1
		n2 = n2 // 2
		print(n2)


for _ in range(int(input())):
	sg = input().strip()
	l = len(sg)
	if l % 2 != 0:
		print(-1)
	else:
		ones = 0
		zeros = 0
		for i in range(l):
			if sg[i] == '1':
				ones += 1
			else:
				zeros += 1
		diff = abs(ones - zeros)
		if diff == l:
			print(-1)
		else:
			diff //= 2
			print(diff)


t = int(input())
for _ in range(t):
	s = input()
	a = s.count('0')
	b = s.count('1')
	if len(s) % 2 == 1 or a == 0 or b == 0:
		ans = -1
	else:
		ans = abs(a - b) // 2
	print(ans)


n = int(input())
for _ in range(n):
	string = input()
	if len(string) % 2 == 1:
		print(-1)
	else:
		if '0' not in string:
			print(-1)
			continue
		if '1' not in string:
			print(-1)
			continue
		c1 = string.count('1')
		c0 = string.count('0')
		res = abs(c1 - c0) // 2
		print(res)