Remove character | Practice | GeeksforGeeks

Given two strings string1 and string2, remove those characters from first string(string1) which are present in second string(string2). Both the strings are different and contain only lowercase characters.
NOTE: Size of  first string is always greater than the size of  second string( |string1| > |string2|).
 
Example 1:
Input:
string1 = "computer"
string2 = "cat"
Output: "ompuer"
Explanation: After removing characters(c, a, t)
from string1 we get "ompuer".
Example 2:
Input:
string1 = "occurrence"
string2 = "car"
Output: "ouene"
Explanation: After removing characters
(c, a, r) from string1 we get "ouene".
 
Your Task:  
You dont need to read input or print anything. Complete the function removeChars() which takes string1 and string2 as input parameter and returns the result string after removing characters from string2  from string1.

Expected Time Complexity:O( |String1| + |String2| )
Expected Auxiliary Space: O(K),Where K = Constant   

Constraints:
1 <= |String1| , |String2| <= 50
0
manavkanjiyanijobupdate2 weeks ago
JAVA ALL CODE TEST CASE PASSED 
class Solution{   static String removeChars(String string1, String string2){      String s = "";      for(int i = 0 ; i < string1.length() ; i++)      {               if(!string2.contains(String.valueOf(string1.charAt(i))))          {              s = s + string1.charAt(i);           }      }      return s;        }}
0
nishraut72 weeks ago
python code:
 
class Solution:   def removeChars (ob, string1, string2):       # code here        for i in string1:           for j in string2:               if i == j:                   string1 = string1.replace(i,"")              return string1
 
0
nithinkirthick3 weeks ago
Python : 0/0.4
    def removeChars (ob, string1, string2):       # code here       elist=[]      for i in string1:          if i not in string2:              elist.append(i)      return "".join(elist)
0
princejee20191 month ago
C++ || USING STL || 0.0 ms || 3 LINE CODE
string removeChars(string s1, string s2) {       for(int i =0;i<s2.size();i++){          s1.erase(remove(s1.begin(),s1.end(),s2[i]),s1.end());       }       return s1;   }
0
pradeepshillare1 month ago
............java(0.2)...
       String v= "";       Stack<Character> s = new Stack<>();       for(int i=0;i<string2.length();i++)       {           s.add(string2.charAt(i));
       }       for(int i=0;i<string1.length();i++)       {           char c = string1.charAt(i);           if(!s.contains(c))           {               v = v + c;           }       }       return v;
0
arjunaryagupta2031 month ago
        #USING ANAGRAM CONCEPT
        #Code in Python       # code here        box=[0]*26       for i in range(len(string1)):           box[ord(string1[i])-ord('a')]=box[ord(string1[i])-ord('a')]+1       for j in range(len(string2)):           if box[ord(string2[j])-ord('a')]!=0:               box[ord(string2[j])-ord('a')]=0              strng=""       for i in range(len(string1)):           if box[ord(string1[i])-ord('a')]!=0:               strng=strng+string1[i]              #print(strng)       return strng
0
meghanareddygopidiPremium1 month ago
class Solution:   def removeChars (ob, string1, string2):       l=[]       for i in string1:           if i not in string2:               l.append(i)           str1 = ""       for ele in l:            str1 += ele        return str1
0
raghavendra7771 month ago
class Solution:   def removeChars (ob, string1, string2):       # code here        s=list(string1)       #print(s)              for ch in string2:           for i in range(0,1000):               if ch in s:                   s.remove(ch)       return ''.join(s)
0
adira42004
This comment was deleted.
0
rahulrs2 months ago
JAVA CODE..................................
static String removeChars(String string1, String string2){       // code here
       HashSet<Character> s2 = new HashSet<>();
       // storing all elements of string2 into s2.       for(int i=0;i<string2.length();i++){           s2.add(string2.charAt(i));       }              String str = "";       for(int i=0;i<string1.length();i++){           if(!s2.contains(string1.charAt(i))){               str+=string1.charAt(i);           }       }       return str;   }
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.