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:
e
Explanation:
'e' is the letter that was added.
題意:t
字符串是 s
中的字符打亂後,再加一個額外的字符形成的。
思路:和136. Single Number一樣的思路和做法。
代碼:
class Solution {
public:
char findTheDifference(string s, string t) {
char c = 0;
for (const auto &ch : s) c ^= ch;
for (const auto &ch : t) c ^= ch;
return c;
}
};
效率:
執行用時:4 ms, 在所有 C++ 提交中擊敗了76.72% 的用戶
內存消耗:6.6 MB, 在所有 C++ 提交中擊敗了100.00% 的用戶