/*
leetcode 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:
e
Explanation:
'e' is the letter that was added.
*/
#include <string>
#include <iostream>
using namespace std;
class Solution {
public:
char findTheDifference(string s, string t)
{
if (s.size() == 0)
return t[0];
int res = 0;
for (auto c : s)
res ^= c;
for (auto c : t)
res ^= c;
return static_cast<char>(res);
}
};
void test()
{
string s("abc");
string t("bacf");
Solution sol;
cout << sol.findTheDifference(s, t) << endl;
}
int main()
{
test();
}
leetcode 389. Find the Difference
最新推荐文章于 2024-03-18 09:56:48 发布