--- title: 'LeetCode 389. Find the Difference' disqus: hackmd --- # LeetCode 389. Find the Difference ## Description You are given two strings s and t. String t is generated by random shuffling string s and then add one more letter at a random position. Return the letter that was added to t. ## Example Input: s = "abcd", t = "abcde" Output: "e" Explanation: 'e' is the letter that was added. Input: s = "", t = "y" Output: "y" ## Constraints 0 <= s.length <= 1000 t.length == s.length + 1 s and t consist of lowercase English letters. ## Answer 此題一樣使用XOR做兩次會不見的概念來先將 s全部做XOR,然後對t做XOR即可得多出來的值。 ```Cin= // 2021_11_07 char findTheDifference(char * s, char * t) { if(*s == '\0'){return *t;} if(*t == '\0'){return *s;} char ans = *s++; while(*s != '\0' || *t != '\0'){ if(*s != '\0'){ans ^= *s++;} if(*t != '\0'){ans ^= *t++;} } return ans; } ``` ## Link https://leetcode.com/problems/find-the-difference/ ###### tags: `Leetcode`