[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.


posted @ 2019-11-13 12:32  xuan_abc  阅读(99)  评论(0编辑  收藏  举报