XOR Strings 3 Hacker Rank Solution Best & Easiest

XOR Strings 3 Problem Solution
XOR Strings 3 Problem Solution

In this post, we will solve XOR Strings 3 HackerRank Solution. This problem (XOR Strings 3) is a part of the HackerRank Problem Solving series.

Problem: https://www.hackerrank.com/challenges/strings-xor/problem

XOR Strings 3 Hacker Rank Solution

Problem solution in JavaScript programming:

function xorstring(s1, s2) {
    let str1 = s1.split('')
    let str2 = s2.split('')
    let result = []
    for (i in str1) {
        if (str1[i] != str2[i]) {
            result.push('1')
        } else {
            result.push('0')
        }
    }
    //console.log(str1, str2,result)
    return result
}

Problem solution in Python programming:

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

    return res

Problem solution in Java programming:

public static String stringsXOR(String s, String t) {
        String res = new String("");
        for(int i = 0; i < s.length(); i++) {
            if(s.charAt(i) == t.charAt(i))
                res += '0';
            else
                res += '1';
        }

        return res;
    }

Problem solution in C++ programming:

string strings_xor(string s, string t) {

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

    return res;
}