Given two strings, s1 and s2. The task is to remove all characters that are common in both strings and then combine the remaining characters from each string to form a new string.
- The characters that are not shared between the two strings should appear in the result in the same order as they appear in their respective original strings.
- If no characters are left after removing the common characters, return "-1"
Examples:
Input: s1 = aacdb, s2 = gafd
Output: cbgf
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.Input: s1 = abcs, s2 = cxzca
Output: bsxz
Explanation: The common characters of s1 and s2 are: a,c. The uncommon characters of s1 and s2 are b,s,x and z. Thus the modified string with uncommon characters concatenated is bsxz.
Table of Content
[Naive Approach] Checking Each Character - O(|s1| * |s2|) Time O(|s1| + |s2|) Space
The idea is to check every character of each string in the other string. If a character is not present in the other string, add it to the result.
Working of Approach:
- Traverse each character of s1 and search for it in s2.
- If the character is not found in s2, append it to res.
- Similarly, traverse each character of s2 and search for it in s1.
- Append the characters that are not present in s1.
- If no character is added to res, return "-1".
#include <iostream>
#include <string>
using namespace std;
string concatenatedString(string &s1, string &s2)
{
string res = "";
// Find characters of s1 that are not present in s2.
for (char c : s1)
{
bool found = false;
// Search for the character in s2.
for (char x : s2)
{
if (c == x)
{
found = true;
break;
}
}
// Append the character if it is not common.
if (!found)
res += c;
}
// Find characters of s2 that are not present in s1.
for (char c : s2)
{
bool found = false;
// Search for the character in s1.
for (char x : s1)
{
if (c == x)
{
found = true;
break;
}
}
// Append the character if it is not common.
if (!found)
res += c;
}
// If no uncommon character exists, return -1.
if (res.empty())
return "-1";
// Return the resulting string.
return res;
}
int main()
{
string s1 = "abcs";
string s2 = "cxzca";
cout << concatenatedString(s1, s2);
return 0;
}
class GFG {
public static String concatenatedString(String s1, String s2) {
String res = "";
// Find characters of s1 that are not present in s2.
for (char c : s1.toCharArray()) {
boolean found = false;
// Search for the character in s2.
for (char x : s2.toCharArray()) {
if (c == x) {
found = true;
break;
}
}
// Append the character if it is not common.
if (!found)
res += c;
}
// Find characters of s2 that are not present in s1.
for (char c : s2.toCharArray()) {
boolean found = false;
// Search for the character in s1.
for (char x : s1.toCharArray()) {
if (c == x) {
found = true;
break;
}
}
// Append the character if it is not common.
if (!found)
res += c;
}
// If no uncommon character exists, return -1.
if (res.isEmpty())
return "-1";
// Return the resulting string.
return res;
}
public static void main(String[] args) {
String s1 = "abcs";
String s2 = "cxzca";
System.out.println(concatenatedString(s1, s2));
}
}
def concatenatedString(s1, s2):
res = ""
# Find characters of s1 that are not present in s2.
for c in s1:
found = False
# Search for the character in s2.
for x in s2:
if c == x:
found = True
break
# Append the character if it is not common.
if not found:
res += c
# Find characters of s2 that are not present in s1.
for c in s2:
found = False
# Search for the character in s1.
for x in s1:
if c == x:
found = True
break
# Append the character if it is not common.
if not found:
res += c
# If no uncommon character exists, return "-1".
if not res:
return "-1"
# Return the resulting string.
return res
if __name__ == '__main__':
s1 = "abcs"
s2 = "cxzca"
print(concatenatedString(s1, s2))
using System;
class GFG {
public string concatenatedString(string s1, string s2) {
string res = "";
// Find characters of s1 that are not present in s2.
foreach (char c in s1) {
bool found = false;
// Search for the character in s2.
foreach (char x in s2) {
if (c == x) {
found = true;
break;
}
}
// Append the character if it is not common.
if (!found)
res += c;
}
// Find characters of s2 that are not present in s1.
foreach (char c in s2) {
bool found = false;
// Search for the character in s1.
foreach (char x in s1) {
if (c == x) {
found = true;
break;
}
}
// Append the character if it is not common.
if (!found)
res += c;
}
// If no uncommon character exists, return -1.
if (res.Length == 0)
return "-1";
// Return the resulting string.
return res;
}
public static void Main() {
string s1 = "abcs";
string s2 = "cxzca";
GFG obj = new GFG();
Console.WriteLine(obj.concatenatedString(s1, s2));
}
}
function concatenatedString(s1, s2)
{
let res = "";
// Find characters of s1 that are not present in s2.
for (let c of s1) {
let found = false;
// Search for the character in s2.
for (let x of s2) {
if (c === x) {
found = true;
break;
}
}
// Append the character if it is not common.
if (!found)
res += c;
}
// Find characters of s2 that are not present in s1.
for (let c of s2) {
let found = false;
// Search for the character in s1.
for (let x of s1) {
if (c === x) {
found = true;
break;
}
}
// Append the character if it is not common.
if (!found)
res += c;
}
// If no uncommon character exists, return "-1".
if (res.length === 0)
return "-1";
// Return the resulting string.
return res;
}
// Driver Code
let s1 = "abcs";
let s2 = "cxzca";
console.log(concatenatedString(s1, s2));
Output
bsxz
[Expected Approach] Using Hash Map - O(|s1| + |s2|) Time and O(|s2|) Space
The idea is to use a hash map to store the characters of s2 and efficiently check whether characters of both strings are common or uncommon.
Working of Approach:
- Store all characters of s2 in an unordered_map.
- Traverse s1 and append characters that are not present in s2.
- Mark common characters with value 2 in the map.
- Traverse s2 and append characters whose map value is still 1.
- If the result is empty, return "-1".
Let us understand with an example:
Input: s1 = abcs, s2 = cxzca
- For s1 = "abcs" and s2 = "cxzca", the map initially stores all characters of s2 as {c:1, x:1, z:1, a:1}.
- Traversing s1, a and c are common, so their values become 2; b and s are not found, so res = "bs".
- Traversing s2, only x and z still have value 1, so they are appended to res.
- Finally, res = "bsxz", which is returned as the answer.
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
string concatenatedString(string &s1, string &s2)
{
unordered_map<char, int> m;
string res = "";
// using map to store all characters of s2 in map.
for (int i = 0; i < s2.size(); i++)
m[s2[i]] = 1;
// finding characters of s1 that are not present in s2
// and appending them to result.
for (int i = 0; i < s1.size(); i++)
{
if (m.find(s1[i]) == m.end())
res += s1[i];
else
m[s1[i]] = 2;
}
// finding characters of s2 that are not present in s1
// and appending them to result.
for (int i = 0; i < s2.size(); i++)
if (m[s2[i]] == 1)
res += s2[i];
if (res == "")
res = "-1";
// returning the result.
return res;
}
int main()
{
string s1 = "abcs";
string s2 = "cxzca";
cout << concatenatedString(s1, s2);
return 0;
}
import java.util.HashMap;
class GFG {
public static String concatenatedString(String s1,
String s2)
{
HashMap<Character, Integer> m = new HashMap<>();
String res = "";
// using map to store all characters of s2 in map.
for (int i = 0; i < s2.length(); i++)
m.put(s2.charAt(i), 1);
// finding characters of s1 that are not present in
// s2 and appending them to result.
for (int i = 0; i < s1.length(); i++) {
if (!m.containsKey(s1.charAt(i)))
res += s1.charAt(i);
else
m.put(s1.charAt(i), 2);
}
// finding characters of s2 that are not present in
// s1 and appending them to result.
for (int i = 0; i < s2.length(); i++)
if (m.get(s2.charAt(i)) == 1)
res += s2.charAt(i);
if (res.equals(""))
res = "-1";
// returning the result.
return res;
}
public static void main(String[] args)
{
String s1 = "abcs";
String s2 = "cxzca";
System.out.println(concatenatedString(s1, s2));
}
}
def concatenatedString(s1, s2):
m = {}
res = ""
# using map to store all characters of s2 in map.
for i in range(len(s2)):
m[s2[i]] = 1
# finding characters of s1 that are not present in s2
# and appending them to result.
for i in range(len(s1)):
if s1[i] not in m:
res += s1[i]
else:
m[s1[i]] = 2
# finding characters of s2 that are not present in s1
# and appending them to result.
for i in range(len(s2)):
if m[s2[i]] == 1:
res += s2[i]
if res == "":
res = "-1"
# returning the result.
return res
if __name__ == '__main__':
s1 = "abcs"
s2 = "cxzca"
print(concatenatedString(s1, s2))
using System;
using System.Collections.Generic;
class GFG {
public string concatenatedString(string s1, string s2)
{
Dictionary<char, int> m
= new Dictionary<char, int>();
string res = "";
// using map to store all characters of s2 in map.
for (int i = 0; i < s2.Length; i++)
m[s2[i]] = 1;
// finding characters of s1 that are not present in
// s2 and appending them to result.
for (int i = 0; i < s1.Length; i++) {
if (!m.ContainsKey(s1[i]))
res += s1[i];
else
m[s1[i]] = 2;
}
// finding characters of s2 that are not present in
// s1 and appending them to result.
for (int i = 0; i < s2.Length; i++)
if (m[s2[i]] == 1)
res += s2[i];
if (res == "")
res = "-1";
// returning the result.
return res;
}
public static void Main()
{
string s1 = "abcs";
string s2 = "cxzca";
GFG obj = new GFG();
Console.WriteLine(obj.concatenatedString(s1, s2));
}
}
function concatenatedString(s1, s2)
{
let m = new Map();
let res = "";
// using map to store all characters of s2 in map.
for (let i = 0; i < s2.length; i++)
m.set(s2[i], 1);
// finding characters of s1 that are not present in s2
// and appending them to result.
for (let i = 0; i < s1.length; i++) {
if (!m.has(s1[i]))
res += s1[i];
else
m.set(s1[i], 2);
}
// finding characters of s2 that are not present in s1
// and appending them to result.
for (let i = 0; i < s2.length; i++)
if (m.get(s2[i]) === 1)
res += s2[i];
if (res === "")
res = "-1";
// returning the result.
return res;
}
// Driver Code
let s1 = "abcs";
let s2 = "cxzca";
console.log(concatenatedString(s1, s2));
Output
bsxz