Intuitive approach.
public class Solution {
public char findTheDifference(String s, String t) {
int[] cnt = new int[26];
for (int i=0; i<s.length(); i++)
cnt[s.charAt(i)-'a']++;
for (int j=0; j<t.length(); j++)
cnt[t.charAt(j)-'a']--;
int k;
for (k=0; k<26; k++)
if (cnt[k] == -1) break;
return (char) ('a' + k);
}
}
Using XOR. Similar with 136. Single Number
public class Solution {
public char findTheDifference(String s, String t) {
int ret = 0;
for (int i=0; i<s.length(); i++)
ret ^= s.charAt(i);
for (int j=0; j<t.length(); j++)
ret ^= t.charAt(j);
return (char) ret;
}
}