Mark and Lightbulbs (Codeforces 1705D)
Tags: Greedy, combinatorics

Mark has just purchased a rack of $$$n$$$ lightbulbs. The state of the lightbulbs can be described with binary string $$$s = s_1s_2\dots s_n$$$, where $$$s_i=\texttt{1}$$$ means that the $$$i$$$-th lightbulb is turned on, while $$$s_i=\texttt{0}$$$ means that the $$$i$$$-th lightbulb is turned off.Unfortunately, the lightbulbs are broken, and the only operation he can perform to change the state of the lightbulbs is the following:  Select an index $$$i$$$ from $$$2,3,\dots,n-1$$$ such that $$$s_{i-1}\ne s_{i+1}$$$.  Toggle $$$s_i$$$. Namely, if $$$s_i$$$ is $$$\texttt{0}$$$, set $$$s_i$$$ to $$$\texttt{1}$$$ or vice versa. Mark wants the state of the lightbulbs to be another binary string $$$t$$$. Help Mark determine the minimum number of operations to do so.

Input: The first line of the input contains a single integer $$$q$$$ ($$$1\leq q\leq 10^4$$$) — the number of test cases.The first line of each test case contains a single integer $$$n$$$ ($$$3\leq n\leq 2\cdot 10^5$$$) — the number of lightbulbs.The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ — the initial state of the lightbulbs.The third line of each test case contains a binary string $$$t$$$ of length $$$n$$$ — the final state of the lightbulbs.It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2\cdot 10^5$$$.

Output: For each test case, print a line containing the minimum number of operations Mark needs to perform to transform $$$s$$$ to $$$t$$$. If there is no such sequence of operations, print $$$-1$$$.

Note: In the first test case, one sequence of operations that achieves the minimum number of operations is the following.   Select $$$i=3$$$, changing $$$\texttt{01}\color{red}{\texttt{0}}\texttt{0}$$$ to $$$\texttt{01}\color{red}{\texttt{1}}\texttt{0}$$$.  Select $$$i=2$$$, changing $$$\texttt{0}\color{red}{\texttt{1}}\texttt{10}$$$ to $$$\texttt{0}\color{red}{\texttt{0}}\texttt{10}$$$.  In the second test case, there is no sequence of operations because one cannot change the first digit or the last digit of $$$s$$$.In the third test case, even though the first digits of $$$s$$$ and $$$t$$$ are the same and the last digits of $$$s$$$ and $$$t$$$ are the same, it can be shown that there is no sequence of operations that satisfies the condition.In the fourth test case, one sequence that achieves the minimum number of operations is the following:   Select $$$i=3$$$, changing $$$\texttt{00}\color{red}{\texttt{0}}\texttt{101}$$$ to $$$\texttt{00}\color{red}{\texttt{1}}\texttt{101}$$$.  Select $$$i=2$$$, changing $$$\texttt{0}\color{red}{\texttt{0}}\texttt{1101}$$$ to $$$\texttt{0}\color{red}{\texttt{1}}\texttt{1101}$$$.  Select $$$i=4$$$, changing $$$\texttt{011}\color{red}{\texttt{1}}\texttt{01}$$$ to $$$\texttt{011}\color{red}{\texttt{0}}\texttt{01}$$$.  Select $$$i=5$$$, changing $$$\texttt{0110}\color{red}{\texttt{0}}\texttt{1}$$$ to $$$\texttt{0110}\color{red}{\texttt{1}}\texttt{1}$$$.  Select $$$i=3$$$, changing $$$\texttt{01}\color{red}{\texttt{1}}\texttt{011}$$$ to $$$\texttt{01}\color{red}{\texttt{0}}\texttt{011}$$$. 

Sample Inputs:
4
4
0100
0010
4
1010
0100
5
01001
00011
6
000101
010011

Sample Outputs:
2
-1
-1
5

Solution (Python):
from sys import stdin, stdout

def get_ints():
    return map(int, stdin.readline().split())

def PRINT(s):
    stdout.write(s + '\n')

def groups(S):
    S += '0'
    start = 0
    end = 0
    growing = False
    ret = []
    for i in range(len(S)):
        if S[i] == '1':
            if growing:
                end = i
            else:
                growing = True
                start = i
                end = i
        else:
            if growing:
                ret.append((start, end))
            growing = False
    return ret

def logic(A, B):
    if A[0] != B[0] or A[-1] != B[-1]:
        return -1
    g1 = groups(A)
    g2 = groups(B)
    if len(g1) != len(g2):
        return -1
    score = 0
    for i in range(len(g1)):
        i1 = g1[i]
        i2 = g2[i]
        score += abs(i1[0] - i2[0]) + abs(i1[1] - i2[1])
    return score
for w in range(int(stdin.readline())):
    n = int(stdin.readline())
    A = stdin.readline()[:-1]
    B = stdin.readline()[:-1]
    print(logic(A, B))

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    a = input()
    b = input()
    a_ = []
    b_ = []
    for i in range(n - 1):
        if a[i] != a[i + 1]:
            a_.append(i)
        if b[i] != b[i + 1]:
            b_.append(i)
    if len(a_) != len(b_) or a[0] != b[0] or a[-1] != b[-1]:
        print(-1)
        continue
    count = 0
    for i in range(len(a_)):
        count += abs(a_[i] - b_[i])
    print(count)

Solution (Python):
def f(s, t, n):
    if s[0] != t[0] or s[n - 1] != t[n - 1]:
        return -1
    (i, j, edits) = (0, 0, 0)
    while True:
        while i < n - 1 and s[i] == s[i + 1]:
            i += 1
        while j < n - 1 and t[j] == t[j + 1]:
            j += 1
        if i < n - 1 and j < n - 1:
            edits += abs(i - j)
        elif i == n - 1 and j == n - 1:
            return edits + abs(i - j)
        else:
            return -1
        i += 1
        j += 1
if __name__ == '__main__':
    T = int(input())
    while T:
        n = int(input())
        (s, t) = (input(), input())
        print(f(s, t, n))
        T -= 1