-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-the-difference.cpp
36 lines (35 loc) · 1.21 KB
/
find-the-difference.cpp
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
//RFER TO THIS YT VIDEO FOR REFERENCE: https://www.youtube.com/watch?v=JyA858J7CrA
//SOME OF THE OPTIMIZED APPROACHS:
//1.XOR OPERATION (GIVEN BELOW)
//2.SUBTRACT THE SUM OF ASCII VALUES
// class Solution {
// public:
// All letters persent in s is present in t.
// There is just one extra letter in t.
// Every character can be represented as an ASCII .
// ASCII value of the extra letter in t is derived as follows:
// Sum of all ASCII values of charcters in s -> sSum
// Sum of all ASCII values of charcters in t -> tSum
// Return the difference (tSum-sSum)
// char findTheDifference(string s, string t) {
// for(int i=0;i<s.size();i++){
// t[i+1] = t[i] - s[i] + t[i+1];
// //passing the diff(t[i]-s[i]) to t[i+1] i.e next element on each iteration
// }
// return t[t.size()-1]; //the difference will be carried over to the last element
// }
// };
//BEST APPROACH - USING XOR OPERATION BITWISE MANIPULATION
class Solution {
public:
char findTheDifference(string s, string t) {
char ans=0;
for(auto x: s){
ans^=x;
}
for(auto x: t){
ans^=x;
}
return ans;
}
};