You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given two strings A and B. Find the characters that are not common in the two strings.
5
+
6
+
Input:
7
+
A = geeksforgeeks
8
+
B = geeksquiz
9
+
Output: fioqruz
10
+
Explanation:
11
+
The characters 'f', 'i', 'o', 'q', 'r', 'u','z'
12
+
are either present in A or B, but not in both.
13
+
14
+
15
+
Input:
16
+
A = characters
17
+
B = alphabets
18
+
Output: bclpr
19
+
Explanation: The characters 'b','c','l','p','r'
20
+
are either present in A or B, but not in both.
21
+
22
+
*/
23
+
24
+
classSolution
25
+
{
26
+
public:
27
+
string UncommonChars(string a, string b)
28
+
{
29
+
// code here
30
+
set <char>s1(a.begin(),a.end());
31
+
set <char>s2(b.begin(),b.end());
32
+
string ans="";
33
+
unordered_map<char,int>m;
34
+
for(auto i:s1)
35
+
{
36
+
m[i]++;
37
+
}
38
+
for(auto i:s2)
39
+
{
40
+
m[i]++;
41
+
}
42
+
for(auto i:m)
43
+
{
44
+
if(i.second==1)
45
+
{
46
+
ans=ans+i.first;
47
+
}
48
+
}
49
+
sort(ans.begin(),ans.end());
50
+
if(ans=="")
51
+
{
52
+
return"-1";
53
+
}
54
+
else
55
+
{
56
+
return ans;
57
+
}
58
+
}
59
+
};
60
+
//conclusion:- using single map if repeated in both string then frequency of that character will be 2, so if it is 1 then make string and sort it . but to avoid duplicates we use set.
0 commit comments