Topic: Strings
Questions Successfully Completed: 1
1)Remove common characters and concatenate | Easy |
Remove common characters and concatenate
Time Complexity : O(N)
Space Complexity: O(Number of distinct characters).
Question
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.
public static String concatenatedString(String s1,String s2)
{
String str ="";
for(int i = 0;i<s1.length();i++){
String s = String.valueOf(s1.charAt(i));
if(!s2.contains(s)){
str =str + s;
}
}
for(int i = 0;i<s2.length();i++){
String s = String.valueOf(s2.charAt(i));
if(!s1.contains(s)){
str =str + s;
}
}
if(str.length()==0){
str = "-1";
}
return str;
}
Thank you for reading :)