Skip to content

Commit 89af43e

Browse files
committed
Remove Common Characters and Concatenate
1 parent e0b3947 commit 89af43e

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

Some_More_Strings/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,11 @@
8989
Input: S = "geeksforgeeks"
9090
Output: g
9191
Explanation: g, e, k and s are the repeating characters. Out of these, g occurs first.
92+
93+
## 11. Remove common characters and concatenate:
94+
Given two strings s1 and s2. Modify both the strings such that all the common characters of s1 and s2 are to be removed and the uncommon characters of s1 and s2 are to be concatenated.
95+
Note: If all characters are removed print -1.
96+
Example 1:
97+
Input: s1 = aacdb, s2 = gafd
98+
Output: cbgf
99+
Explanation: The common characters of s1 and s2 are: a, d. The uncommon characters of s1 and s2 are c, b, g and f. Thus the modified string with uncommon characters concatenated is cbgf.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// { Driver Code Starts
2+
// C++ program Find concatenated string with
3+
// uncommon characters of given strings
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
8+
// } Driver Code Ends
9+
10+
11+
class Solution
12+
{
13+
public:
14+
//Function to remove common characters and concatenate two strings.
15+
string concatenatedString(string s1, string s2)
16+
{
17+
// Your code here
18+
string s = "";
19+
int map1[26] = {0}, map2[26] = {0};
20+
for(char c : s2)
21+
map1[c - 'a']++;
22+
for(char c : s1)
23+
{
24+
if(map1[c - 'a'] == 0)
25+
s += c;
26+
}
27+
for(char c : s1)
28+
map2[c - 'a']++;
29+
for(char c : s2)
30+
{
31+
if(map2[c - 'a'] == 0)
32+
s += c;
33+
}
34+
return s.length() == 0 ? "-1" : s;
35+
}
36+
37+
};
38+
39+
// { Driver Code Starts.
40+
41+
/* Driver program to test above function */
42+
int main()
43+
{
44+
int t;
45+
cin >> t;
46+
47+
while(t--){
48+
string s1, s2;
49+
cin >> s1 >> s2;
50+
Solution obj;
51+
string res = obj.concatenatedString(s1, s2);
52+
cout<<res<<endl;
53+
}
54+
return 0;
55+
}
56+
// } Driver Code Ends

0 commit comments

Comments
 (0)