389 Find the Difference
Bit Manipulation XOR 相同为0 不同为1
Given two stringssandtwhich consist of only lowercase letters.
Stringtis generated by random shuffling stringsand then add one more letter at a random position.
Find the letter that was added int.
Example:
Input:
s = "abcd"
t = "abcde"
Output:
e
Explanation:
'e' is the letter that was added.
time O(n)
space O(1)
class Solution {
public char findTheDifference(String s, String t) {
char c = t.charAt(t.length() - 1);
for (int i = 0; i < s.length(); i++) {
c ^= s.charAt(i);
c ^= t.charAt(i);
}
return c;
}
}