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% 的用户