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.
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.
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 email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Concatenated string with uncommon characters in Python
- Find uncommon characters of the two strings
- Find uncommon characters of the two strings | Set 2
- Python program to find uncommon words from two Strings
- Print the final string when minimum value strings get concatenated in every operation
- Reorder the given string to form a K-concatenated string
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Nth character in Concatenated Decimal String
- Print Kth character in sorted concatenated substrings of a string
- Maximum Consecutive Zeroes in Concatenated Binary String
- Shortest Uncommon Subsequence
- Longest Uncommon Subsequence
- Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated
- Count of pairs of Array elements which are divisible by K when concatenated
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Check if a string can be split into two strings with same number of K-frequent characters
- Strings formed from given characters without any consecutive repeating characters
- Meta Strings (Check if two strings can become same after a swap in one string)
- Create a new string by alternately combining the characters of two halves of the string in reverse