Remove characters from the first string which are present in the second string

PROBLEM :

Given two strings s1 & s2, remove those characters from first string which are present in second string. Both the strings are different and contain only lowercase characters.

Input:

The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is s1,s1 is first string.
The second line of each test case contains s2,s2 is second string.

Output:

Print the modified string(s1). For each test case, print the output in a new line.

Constraints:

1 = T = 15
1 = s2 < s1 = 50

Example:

Input:

2
geeksforgeeks
mask
removeccharaterfrom
string

Output:

geeforgee

emovecchaaefom

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
#include<stdlib.h>
void Remove_character(char *,char *) ;
int main()
 {
int t ;
char str1[50],str2[50] ;
cin>>t ;
while(t--)
{
   cin>>str1>>str2 ;
   Remove_character(str1,str2) ;
   cout<<str1<<endl ;
}
return 0;
}

void Remove_character(char *str1,char *str2)
{
    int *count ;
    char *k ;
    count=(int*)calloc(sizeof(int),256) ;
    k=str2 ;
    while((*k)!='\0')
    {
        count[*k]++ ;
        *(k++) ;
    }
   
    int i=0,j=0 ;
   
    while(*(str1+i)!='\0')
    {
        if(count[*(str1+i)]==0)
        {
            *(str1+j)=*(str1+i) ;
            j++ ;
        }
        i++ ;
    }
    *(str1+j)='\0' ;
}
--------------------------------------------------------------------------------
BETTER SOLUTION :(hashing)
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
#include<string.h>
int main()
{
int t;
cin>>t;
while(t--)
{
   string s1,s2,s3;
   int i,j,l1,l2;
 
   cin>>s1>>s2;
 
   l1=s1.length();
   l2=s2.length();
 
   int hash[257];
   for(i=0;i<257;i++)
       hash[i]=0;
 
   for(i=0;i<l2;i++)
       hash[s2[i]]++;
     
   for(i=0;i<l1;i++)
   {
       if(hash[s1[i]]==0)
       s3=s3+s1[i];
   }
 
   cout<<s3<<endl;
  }
return 0;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Primality Test ( CodeChef Problem code: PRB01 )