Concatenated string with uncommon characters of two strings | Java program Find concatenated string with uncommon characters of given strings ; Result ; creating a hashMap to add characters in string s2 ; 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 code
import java . util . * ; import java . lang . * ; import java . io . * ; class gfg { public static String concatenatedString ( String s1 , String s2 ) { String res = " " ; int i ; HashMap < Character , Integer > m = new HashMap < Character , Integer > ( ) ; for ( i = 0 ; i < s2 . length ( ) ; i ++ ) m . put ( s2 . charAt ( i ) , 1 ) ; for ( i = 0 ; i < s1 . length ( ) ; i ++ ) if ( ! m . containsKey ( s1 . charAt ( i ) ) ) res += s1 . charAt ( i ) ; else m . put ( s1 . charAt ( i ) , 2 ) ; for ( i = 0 ; i < s2 . length ( ) ; i ++ ) if ( m . get ( s2 . charAt ( i ) ) == 1 ) res += s2 . charAt ( i ) ; return res ; } public static void main ( String [ ] args ) { String s1 = " abcs " ; String s2 = " cxzca " ; System . out . println ( concatenatedString ( s1 , s2 ) ) ; } }