forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Difference_of_Two_Strings.cpp
66 lines (58 loc) · 1.31 KB
/
Difference_of_Two_Strings.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
DIFFERENCE OF TWO STRINGS
Problem Statement
Given 2 strings a and b. String b has one more char added and shuffled. Find the char added in the string b.
Implementation
a. Using Hashmap (Slow approach and extra space)
b. Using Bits Manipulation (Efficient Approach and no extra space)
*/
#include<bits/stdc++.h>
using namespace std;
// Efficient Approach (Space - O(1) and time - O(N))
char bits_diff(string a, string b)
{
char xorr = 0;
int i = 0;
for(i=0;i<a.size();i++){
xorr ^= (a[i] ^ b[i]);
}
return xorr ^ b[i];
}
// Space - O(N) and Time - O(M+N) where m and n are the size of the 2 strings
char map_diff(string a, string b) {
unordered_map<char,int> mp1, mp2;
for(auto x:a)
mp1[x]++;
for(auto y:b)
mp2[y]++;
for(auto x:b)
{
if(mp1.find(x)==mp2.end()) // finds the char of mp1 in mp2
return x;
else if(mp1[x]!=mp2[x]) // a = 's' and b = 'ss' for the same char but diff count ans = 's'
return x;
}
return b[(b.size())-1];
}
int main()
{
string a,b;
cin>>a>>b;
cout<< bits_diff(a,b) << endl;
cout<< map_diff(a,b) << endl;
return 0;
}
/*
Input
a = "abc"
b = "abcd"
Output: d
Input
a = ""
b = "c"
Output: c
Input
a = "zz"
b = "zzz"
Output: z
*/