[LC] 389. Find the Difference

Given two strings s and t which consist of only lowercase letters.

String t is generated by random shuffling string s and then add one more letter at a random position.

Find the letter that was added in t.

Example:

Input:
s = "abcd"
t = "abcde"

Output:
 1 class Solution {
 2     public char findTheDifference(String s, String t) {
 3         int res = 0;
 4         for (char c : s.toCharArray()) {
 5             res ^= c - 'a'; 
 6         }
 7         for (char ch : t.toCharArray()) {
 8             res ^= ch - 'a';
 9         }
10         return (char)(res + 'a');
11     }
12 }
e

Explanation:
'e' is the letter that was added.


猜你喜欢

转载自www.cnblogs.com/xuanlu/p/11848048.html