Java 8 Solution


  • 0
    A
    import java.util.concurrent.atomic.AtomicInteger;
    
    public class Solution {
        public char findTheDifference(String s, String t) {
            //Solution 1
            AtomicInteger counter = new AtomicInteger(0);
            
            final int[] res = { (int) t.charAt(counter.get()) };
            
            s.chars().forEach(ch -> res[0] ^= ch ^ t.charAt(counter.incrementAndGet()));
            return (char) res[0];
            //End Solution 1
            
            //Solution 2
            // return  (char)(t.chars().reduce(0, (Integer::sum)) - s.chars().reduce(0, (Integer::sum)));
            //End Solution 2
        }
    }
    

Log in to reply
 

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