Concatenated string with uncommon characters of two strings | C ++ program Find concatenated string with uncommon characters of given strings ; store all characters of s2 in map ; Find characters of s1 that are not present in s2 and append to result ; Find characters of s2 that are not present in s1 . ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; string concatenetedString ( string s1 , string s2 ) { unordered_map < char , int > m ; for ( int i = 0 ; i < s2 . size ( ) ; i ++ ) m [ s2 [ i ] ] = 1 ; for ( int i = 0 ; i < s1 . size ( ) ; i ++ ) { if ( m . find ( s1 [ i ] ) == m . end ( ) ) res += s1 [ i ] ; else m [ s1 [ i ] ] = 2 ; } for ( int i = 0 ; i < s2 . size ( ) ; i ++ ) if ( m [ s2 [ i ] ] == 1 ) res += s2 [ i ] ; return res ; } int main ( ) { string s1 = " abcs " ; string s2 = " cxzca " ; cout << concatenetedString ( s1 , s2 ) ; return 0 ; }