Friendly Binary Strings
Source: codechef
Difficulty: 1772

Friendly Binary Strings
You are given two binary strings$A$and$B$of length$N$. You are allowed to perform the following operations any number of times (possibly zero):
Choose any index$i$and swap the characters$A_i$and$B_i$.
Choose any two indices$i$and$j$and simultaneously swap$A_i$with$A_j$and$B_i$with$B_j$.
Determine if it is possible to perform a sequence of these operations such that both$A$and$B$become
palindromes
.
Input Format
The first line of input will contain a single integer$T$, denoting the number of test cases.
Each test case consists of three lines:
The first line of each test case contains$N$— the length of the strings.
The second line contains the binary string$A$.
The third line contains the binary string$B$.
Output Format
For each test case, output a single line containing
YES
if it is possible to make both strings palindromic using the given operations, or
NO
otherwise.
You may print each character of the string in uppercase or lowercase (for example, the strings
YES
,
yEs
,
yes
, and
yeS
will all be treated as identical).
Constraints$1 \leq T \leq 10^5$$1 \leq N \leq 2\cdot 10^5$The sum of$N$over all test cases won't exceed$2\cdot 10^5$.
Sample 1:
Explanation:
Test Case 2:
There can be many other ways; here is one:
Choose$i=1$and swap$A_1$and$B_1$, which gives$A=00$and$B=11$.
Test Case 3:
It can be proven that, no matter what operations we apply, we cannot make both$A$and$B$palindromes.
Test Case 4:
There can be many other ways; here is one:
Choose$i=2$and$j=3$and swap$A_2$with$A_3$and$B_2$with$B_3$, which gives$A=101$and$B=101$.
Test Case 5:
There can be many other ways; here is one:
Choose$i=3$and$j=4$and swap$A_2$with$A_3$and$B_2$with$B_3$, which gives$A=1000$and$B=0001$.
Choose$i=4$and swap$A_4$and$B_4$, which gives$A=1001$and$B=0000$.

Sample Input:
5
1
1
0
2
10
01
2
10
10
3
110
110
4
1000
0010

Sample Output:
YES
YES
NO
YES
YES

C++ solution:
/*   Author: Arnab Saha  Date: 05/03/2025 [02:07:08]   */
#include <bits/stdc++.h>
using namespace std;

#define fast_IO                       \
    ios_base::sync_with_stdio(false); \
    cin.tie(nullptr);                 \
    cout.tie(nullptr)
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define endl '\n'
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define rrep(i, a, b) for (int i = a; i >= b; --i)
#define each(x, a) for (auto &x : a)
#define yes cout << "YES" << endl
#define no cout << "NO" << endl

const int inf = 1e9;
const ll linf = 1e18;
const int mod = 1e9 + 7;

void arnabsahawrk()
{
    ll n;
    cin >> n;

    string s, t;
    cin >> s >> t;

    ll z = 0, o = 0, diff = 0;

    rep(i, 0, n)
    {
        if (s[i] == t[i])
        {
            if (s[i] == '0')
                z++;
            else
                o++;
        }
        else
            diff++;
    }

    (z % 2) + (o % 2) + (diff % 2) <= 1 ? yes : no;
}

int main()
{
    fast_IO;

    int tc = 1;
    cin >> tc;
    while (tc--)
        arnabsahawrk();

    return 0;
}

Go solution:
package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
)

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    const maxCapacity = 1024 * 1024
    buf := make([]byte, maxCapacity)
    scanner.Buffer(buf, maxCapacity)
    scanner.Split(bufio.ScanWords)
    scanner.Scan()
    t, _ := strconv.Atoi(scanner.Text())
    results := make([]string, t)
    for i := 0; i < t; i++ {
        scanner.Scan()
        n, _ := strconv.Atoi(scanner.Text())
        scanner.Scan()
        A := scanner.Text()
        scanner.Scan()
        B := scanner.Text()
        c00, c11, c01 := 0, 0, 0
        for j := 0; j < n; j++ {
            if A[j] == B[j] {
                if A[j] == '0' {
                    c00++
                } else {
                    c11++
                }
            } else {
                c01++
            }
        }
        if n%2 == 0 {
            if c00%2 == 0 && c11%2 == 0 && c01%2 == 0 {
                results[i] = "YES"
            } else {
                results[i] = "NO"
            }
        } else {
            oddTotal := (c00 % 2) + (c11 % 2) + (c01 % 2)
            if oddTotal == 1 {
                results[i] = "YES"
            } else {
                results[i] = "NO"
            }
        }
    }
    writer := bufio.NewWriter(os.Stdout)
    for _, res := range results {
        fmt.Fprintln(writer, res)
    }
    writer.Flush()
}

Java solution:
/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
    public static void main(String[] args) {
        int t = sc.nextInt();
        while (t-- > 0) {
            int n = sc.nextInt();
            String s1 = sc.next();
            String s2 = sc.next();
            int zero = 0;
            int one = 0;
            int two = 0;
            for(int i=0;i<n;i++){
                char c1 = s1.charAt(i);
                char c2 = s2.charAt(i);
                int cnt = 0;
                if(c1=='1') cnt++;
                if(c2=='1') cnt++;
                if(cnt==0) zero++;
                else if(cnt==1) one++;
                else two++;
            }
            int c = 0;
            if(zero%2==1) c++;
            if(one%2==1) c++;
            if(two%2==1) c++;
            if(c<2) System.out.println("YES");
            else System.out.println("NO");
        }
    }
    
    public static void input(Object array) {
        if (array instanceof int[])
            for (int i = 0; i < ((int[]) array).length; i++)
                ((int[]) array)[i] = sc.nextInt();
        else if (array instanceof long[])
            for (int i = 0; i < ((long[]) array).length; i++)
                ((long[]) array)[i] = sc.nextLong();
    }
    
    public static void printArr(Object array) {
        if (array instanceof int[])
            for (int x : (int[]) array)
                System.out.print(x + " ");
        else if (array instanceof long[])
            for (long x : (long[]) array)
                System.out.print(x + " ");
        System.out.println();
    }
    
    public static <T extends Number> long gcd(T a, T b) {
        return b.longValue() == 0 ? a.longValue() : gcd(b, a.longValue() % b.longValue());
    }
    
    static FastReader sc = new FastReader();
    
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;
    
        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
    
        String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
    
        int nextInt() {
            return Integer.parseInt(next());
        }
    
        long nextLong() {
            return Long.parseLong(next());
        }
    
        double nextDouble() {
            return Double.parseDouble(next());
        }
    
        String nextLine() {
            String str = "";
            try {
                str = br.readLine().trim();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}

JavaScript solution:
process.stdin.resume();
process.stdin.setEncoding('utf8');

let input = '';
process.stdin.on('data', chunk => input += chunk);
process.stdin.on('end', () => {
    let [t, ...data] = input.trim().split("\n"), res = [];
    for (let i = 0, j = 0; i < t; i++) {
        let n = +data[j++], a = data[j++], b = data[j++], c0 = 0, c1 = 0, c2 = 0;
        for (let k = 0; k < n; k++) (a[k] === b[k]) ? (a[k] === '0' ? c0++ : c1++) : c2++;
        res.push((n % 2 === 0) ? ((c0 % 2 === 0 && c1 % 2 === 0 && c2 % 2 === 0) ? "YES" : "NO") : ((c0 % 2 + c1 % 2 + c2 % 2 === 1) ? "YES" : 
            "NO"));
    }
    console.log(res.join("\n"));
});

Python solution:
# cook your dish here
def shayad_hojaye():
    import sys
    data = sys.stdin.read().strip().split()
    t = int(data[0])
    i = 1
    for _ in range(t):
        N = int(data[i]); i += 1
        A = data[i]; i += 1
        B = data[i]; i += 1
        qq1 = qq2 = qq3 = 0
        for k in range(N):
            x = A[k]
            y = B[k]
            if x == '0' and y == '0':
                qq1 += 1
            elif x == '1' and y == '1':
                qq2 += 1
            else:
                qq3 += 1
        if N % 2 == 0:
            if qq1 % 2 == 0 and qq2 % 2 == 0 and qq3 % 2 == 0:
                print("YES")
            else:
                print("NO")
        else:
            odd_hai = (qq1 % 2) + (qq2 % 2) + (qq3 % 2)
            if odd_hai <= 1:
                print("YES")
            else:
                print("NO")

shayad_hojaye()

Ruby solution:
def solve
  t = gets.to_i
  t.times do
    n = gets.to_i
    a = gets.chomp
    b = gets.chomp
    c0 = 0
    c1 = 0
    c2 = 0
    (0...n).each do |i|
      sum = a[i].to_i + b[i].to_i
      case sum
      when 0
        c0 += 1
      when 1
        c1 += 1
      when 2
        c2 += 1
      end
    end
    count_odd = 0
    count_odd += 1 if c0 % 2 != 0
    count_odd += 1 if c1 % 2 != 0
    count_odd += 1 if c2 % 2 != 0
    if count_odd <= 1
      puts "YES"
    else
      puts "NO"
    end
  end
end

solve