/*
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
最新推荐文章于 2025-02-20 00:20:29 发布
本文介绍了一个简单的C++程序来解决LeetCode上的题目“Find the Difference”。该程序通过异或运算找出被添加到字符串t中的额外字母。示例展示了如何使用这个算法找到区别于字符串s的额外字符。
393

被折叠的 条评论
为什么被折叠?



