My 7ms Java solution using Bit Manipulation


  • 0
    C

    Here is my code
    Also looking for a more elegant way.
    Happy coding:)

    public class Solution {
        public char findTheDifference(String s, String t) {
            char diff = 0;
            
            int index = 0;
            
            if(t.length() < s.length() ){
                while(index < s.length()){
                    if(index < t.length())
                        diff ^= t.charAt(index);
                    diff ^= s.charAt(index);
                    ++index;
                }
            }
            else{
                while(index < t.length()){
                    if(index < s.length())
                        diff ^= s.charAt(index);
                    diff ^= t.charAt(index);
                    ++index;
                }
            }
                
            
            return diff;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.