Header Ad

HackerRank XOR Strings 2 problem solution

In this HackerRank XOR Strings 2 problem solution, we have given Given two strings consisting of digits 0 and 1 only, find the XOR of the two strings.

hackerrank XOR strings problem solution


Problem solution in Python.

def strings_xor(s, t):
    res = ""
    for i in range(len(s)):
        if bool(1-int(s[i])^int(t[i])):
            res += '0';
        else:
            res += '1';

    return res

s = input()
t = input()
print(strings_xor(s, t))


Problem solution in C++.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

string strings_xor(string s, string t) {

    string res = "";
    for(int i = 0; i < s.size(); i++) {
        if(s[i] == t[i])
            res += '0';
        else
            res += '1';
    }

    return res;
}

int main() {
    string s, t;
    cin >> s >> t;
    cout << strings_xor(s, t) << endl;
    return 0;
}


Post a Comment

0 Comments