Concatenated string with uncommon characters of two strings
Two strings are given and you have to modify 1st string such that all the common characters of the 2nd strings have to be removed and the uncommon characters of the 2nd string have to be concatenated with uncommon characters of the 1st string.
Examples:
Input : S1 = "aacdb"
S2 = "gafd"
Output : "cbgf"
Input : S1 = "abcs";
S2 = "cxzca";
Output : "bsxz"
The idea is to use hash map where key is character and value is number of strings in which character is present. If a character is present in one string, then count is 1, else if character is present in both strings, count is 2.
- Initialize result as empty string.
- Push all characters of 2nd string in map with count as 1.
- Traverse first string and append all those characters to result that are not present in map. Characters that are present in map, make count 2.
- Traverse second string and append all those characters to result whose count is 1.
C++
// C++ program Find concatenated string with // uncommon characters of given strings #include <bits/stdc++.h> using namespace std; string concatenetedString(string s1, string s2) { string res = ""; // result // store all characters of s2 in map unordered_map<char, int> m; for (int i = 0; i < s2.size(); i++) m[s2[i]] = 1; // Find characters of s1 that are not // present in s2 and append 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; } // Find characters of s2 that are not // present in s1. for (int i = 0; i < s2.size(); i++) if (m[s2[i]] == 1) res += s2[i]; return res; } /* Driver program to test above function */int main() { string s1 = "abcs"; string s2 = "cxzca"; cout << concatenetedString(s1, s2); return 0; } |
Java
// Java program Find concatenated string with // uncommon characters of given strings import java.util.*; import java.lang.*; import java.io.*; class gfg { public static String concatenatedString(String s1, String s2) { // Result String res = ""; int i; // creating a hashMap to add characters in string s2 HashMap<Character, Integer> m = new HashMap<Character, Integer>(); for (i = 0; i < s2.length(); i++) m.put(s2.charAt(i), 1); // Find characters of s1 that are not // present in s2 and append to result for (i = 0; i < s1.length(); i++) if (!m.containsKey(s1.charAt(i))) res += s1.charAt(i); else m.put(s1.charAt(i), 2); // Find characters of s2 that are not // present in s1. for (i = 0; i < s2.length(); i++) if (m.get(s2.charAt(i)) == 1) res += s2.charAt(i); return res; } // Driver code public static void main(String[] args) { String s1 = "abcs"; String s2 = "cxzca"; System.out.println(concatenatedString(s1, s2)); } } /* This code is contributed by Devarshi_Singh*/ |
Python 3
# Python3 program Find concatenated string # with uncommon characters of given strings def concatenetedString(s1, s2): res = "" # result m = {} # store all characters of s2 in map for i in range(0, len(s2)): m[s2[i]] = 1 # Find characters of s1 that are not # present in s2 and append to result for i in range(0, len(s1)): if(not s1[i] in m): res = res + s1[i] else: m[s1[i]] = 2 # Find characters of s2 that are not # present in s1. for i in range(0, len(s2)): if(m[s2[i]] == 1): res = res + s2[i] return res # Driver Code if __name__ == "__main__": s1 = "abcs" s2 = "cxzca" print(concatenetedString(s1, s2)) # This code is contributed # by Sairahul099 |
Output:
bsxz
This article is contributed by Harshit Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Concatenated string with uncommon characters in Python
- Find uncommon characters of the two strings
- Find uncommon characters of the two strings | Set 2
- Print the final string when minimum value strings get concatenated in every operation
- Python program to find uncommon words from two Strings
- Find the number of strings formed using distinct characters of a given string
- Reorder the given string to form a K-concatenated string
- Nth character in Concatenated Decimal String
- Print Kth character in sorted concatenated substrings of a string
- Maximum Consecutive Zeroes in Concatenated Binary String
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Min flips of continuous characters to make all characters same in a string
- String with k distinct characters and no same characters adjacent
- Meta Strings (Check if two strings can become same after a swap in one string)



