-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path389-Find-the-Difference.cpp
More file actions
38 lines (30 loc) · 1005 Bytes
/
389-Find-the-Difference.cpp
File metadata and controls
38 lines (30 loc) · 1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Author : Vicen-te
// Date : 09/09/2023
/*
Problem 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.
Ex1:
Input: s = "abcd", t = "abcde"
Output: "e"
Explanation: 'e' is the letter that was added.
Ex2:
Input: s = "", t = "y"
Output: "y"
Algorithm:
For each character in both strings 's' and 't,' perform an XOR operation, obtaining the character that has been added to 't'.
Time Complexity: O(N) - size of the string (s+t)
Space Complexity: O(1) - we only use a single char
*/
class Solution {
public:
char findTheDifference(string s, string t) {
char difference = 0;
for (char character : s+t)
{
difference ^= character;
}
return difference;
}
};