XOR Strings 2

  • + 2 comments

    "I've been working on the function and encountered an issue that I haven't been able to resolve. Here's the code I'm using:

    Java 7

    public class Solution {

    public static String stringsXOR(String s, String t) {
        StringBuilder res = new StringBuilder(); // Use StringBuilder for efficient concatenation
    
        for (int i = 0; i < s.length(); i++) {
        // Calculate XOR for each pair of characters
            res.append(s.charAt(i) ^ t.charAt(i));
        }
    
        return res.toString().trim();
    }
    
    public static void main(String[] args) {
    
        String s, t;
        Scanner in = new Scanner(System.in);
        s = in.nextLine();
        t = in.nextLine();
        System.out.print(stringsXOR(s, t));
    

    It passes basic test cases manually, but when running in the environment, it fails with a 'Wrong Answer' message.

    I can't figure out where the issue lies, as both the outputs seem the same. If anyone can provide insights or point out anything I might have missed, I'd really appreciate the help. Thanks in advance!"

    }
    

    }