Remove common characters and concatenate | Practice | GeeksforGeeks

Given two strings s1 and s2. Modify both the strings such that all the common characters of s1 and s2 are to be removed and the uncommon characters of s1 and s2 are to be concatenated.
Note: If all characters are removed print -1.
Example 1:
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.

Example 2:
Input:
s1 = abcs
s2 = cxzca
Output: bsxz
Explanation: The common characters of s1
and s2 are: a,c. The uncommon characters
of s1 and s2 are b,s,x and z. Thus the
modified string with uncommon characters
concatenated is bsxz.
Your Task:
The task is to complete the function concatenatedString() which removes the common characters, concatenates, and returns the string. If all characters are removed then return -1.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Number of distinct characters).
Note: N = |Length of Strings|
Constraints: 
1 <= |Length of Strings| <= 104
0
abhishekaich200021 hours ago
c++ solution using hashing technique
 
string concatenatedString(string s1, string s2)    {        // Your code here       string s="";       string sum=s1+s2;       int H1[26]={0},H2[26]={0};       for(int i=0;i<s1.length();i++){           H1[s1[i]-97]++;       }       for(int i=0;i<s2.length();i++){           H2[s2[i]-97]++;       }       for(int i=0;i<sum.length();i++){           if(H2[sum[i]-97]==0){               s+=sum[i];           }else if(H1[sum[i]-97]==0){               s+=sum[i];           }       }       if(s==""){           return "-1";       }else{           return s;       }   }
0
abhirock6181 week ago
 def concatenatedString(self,s1,s2):       j=s1+s2       for i in j:           if(i in s1 and i in s2):              j= j.replace(i,'')       if(len(j)!=0):           return j       return -1
0
sunghunet3 weeks ago
    def concatenatedString(self,s1,s2):
        
        common_set=set(s1).intersection(set(s2))
        result=[]
        
        for v in list(s1+s2):
            if v in common_set:
                continue
            result.append(v)
        
        if len(result) > 0:
            sorted(result)
        else:
            return str(-1)
        
        return "".join(result)
0
mkamaalg41 month ago
public static String concatenatedString(String s1,String s2)   {       String res = "";
       for (Character c: s1.toCharArray()) {           if (!s2.contains(c.toString())) {               res = res+c.toString();           }       }       for (Character c: s2.toCharArray()) {           if (!s1.contains(c.toString())) {               res = res+c.toString();           }       }
       if (res.length() <= 0) {           return "-1";       }       else           return res;   }
0
bikrantsarmah2 months ago
 string concatenatedString(string s1, string s2)    {        // Your code here              set<char> s;
int i,j,k;
string res="";for(i=0;i<s2.length();i++)   s.insert(s2[i]);
 
for(i=0;i<s1.length();i++)   if(!s.count(s1[i]))   res+=s1[i];
   set<char> q;
   for(i=0;i<s1.length();i++)   q.insert(s1[i]);
    for(i=0;i<s2.length();i++)   if(!q.count(s2[i]))   res+=s2[i];      if(res.length()==0)   return "-1";         return res;          }
0
im_kira2 months ago
//javascript solution
class Solution {   //Function to remove common characters and concatenate two strings.   computeHash(s1){       const m =new Map();         for(let i=0;i<s1.length;i++){           if(m.has(s1[i])){               let cnt=m.get(s1[i])               cnt++;               m.set(s1[i],cnt)           }           else{               m.set(s1[i],1)           }       }       return m;   }   removeString(s1,p,m1){            for(let i=0;i<s1.length;i++){           if(!m1.has(s1[i]))p=p+s1[i]       }       return p;   }   concatenatedString(s1, s2)   {       const m=this.computeHash(s1);       const m1=this.computeHash(s2);       let ans=this.removeString(s1,'',m1)       ans=this.removeString(s2,ans,m)       return ans===''?-1:ans;   }}
0
madhajaswanth2 months ago
A simple solution without any use of Maps and Sets
 
class Solution{     public static String concatenatedString(String s1,String s2)   {             String res = "";      
      // checks if each character in s1 is in s2 or not
       // if not present then it is added to Result      for(int i=0;i<s1.length();i++){          if(!s2.contains(String.valueOf(s1.charAt(i)))){              res += s1.charAt(i);          }      }            for(char ch : s2.toCharArray()){          if(!s1.contains(String.valueOf(ch))){              res += ch;          }      }            if(res == ""){          return "-1";      }      return res;   }}
+1
tarunkanade2 months ago
0.9/2.3
public static String concatenatedString(String s1,String s2)
    {
        // Your code here
        String res = "";
        
        HashSet<Character> set1 = new HashSet<>();
        HashSet<Character> set2 = new HashSet<>();
        
        for(int i=0;i<s1.length();i++){
            set1.add(s1.charAt(i));
        }
        
        for(int i=0;i<s2.length();i++){
            set2.add(s2.charAt(i));
        }
        
        for(int i=0;i<s1.length();i++){
            if(!set2.contains(s1.charAt(i))){
                res += s1.charAt(i);
            }
        }
        
        for(int i=0;i<s2.length();i++){
            if(!set1.contains(s2.charAt(i))){
                res += s2.charAt(i);
            }
        }
        
        if(res.length()>0){
            return res;
        }else{
            return "-1";
        }
    } 
0
tarunkanade2 months ago
0.8/2.3
public static String concatenatedString(String s1,String s2)
    {
        // Your code here
        String res = "";
        
        int []count1 = new int[132];
        int []count2 = new int[132];
        
        for(int i=0; i<s1.length(); i++){
            count1[s1.charAt(i)]++;
        }
        
        for(int i=0; i<s2.length(); i++){
            count2[s2.charAt(i)]++;
        }
        
        for(int i=0; i<s1.length(); i++){
            if(count2[s1.charAt(i)] == 0){
                res += s1.charAt(i);
            }
        }
        for(int i=0; i<s2.length(); i++){
            if(count1[s2.charAt(i)] == 0){
                res += s2.charAt(i);
            }
        }
        
        if(res.length()>0){
            return res;
        }else{
            return "-1";
        }
    }
0
jatinbitthal13193 months ago
java 1.4 sec 
class Solution{   //Function to remove common characters and concatenate two strings.   public static String concatenatedString(String s1,String s2)   {       // Your code here       String answer = s1 + s2;       String answer1 =  s1 + s2;       String result = "";       for (int i = 0; i < s1.length(); i++) {           for (int j = 0; j < answer.length(); j++) {               if (s1.charAt(i) == answer.charAt(j)) {                   answer = answer.replaceAll(Character.toString(answer.charAt(j)), "");               }           }       }       for (int i = 0; i < s2.length(); i++) {           for (int j = 0; j < answer1.length(); j++) {               if (s2.charAt(i) == answer1.charAt(j)) {                   answer1 = answer1.replaceAll(Character.toString(answer1.charAt(j)), "");               }           }       }       answer1 += answer;       if (answer1.equals("")) {           return "-1";       } else {          return answer1;        }          }} 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.