Skip to content

Commit 3e5b507

Browse files
committed
my commit
1 parent 4f40051 commit 3e5b507

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

uncommen_character.cpp

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/* link:- https://practice.geeksforgeeks.org/problems/uncommon-characters4932/1
2+
3+
problem:-
4+
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+
class Solution
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.
61+
62+
63+
/*
64+
65+
OTHER APPROCH:-
66+
67+
68+
class Solution
69+
{
70+
public:
71+
string UncommonChars(string a, string b)
72+
{
73+
// code here
74+
75+
int mp1[26]={0} , mp2[26]={0};
76+
// int n=a.length(),b.length();
77+
for(auto &x:a)
78+
{
79+
mp1[x-'a']=1;
80+
}
81+
for(auto &x:b)
82+
{
83+
mp2[x-'a']=1;
84+
}
85+
string ans="";
86+
87+
for(int i=0;i<26;i++)
88+
{
89+
if(mp1[i]^mp2[i])
90+
{
91+
ans+=char(i+'a');
92+
}
93+
}
94+
if(ans=="")
95+
{
96+
return "-1";
97+
}
98+
else
99+
{
100+
return ans;
101+
}
102+
}
103+
};
104+
105+
*/

0 commit comments

Comments
 (0)