Remove characters from the first string which are present in the second string
Write an efficient C function that takes two strings as arguments and removes the characters from the first string which are present in the second string (mask string).
We strongly recommend that you click here and practice it, before moving on to the solution.
Algorithm: Let the first input string be a ”test string” and the string which has characters to be removed from the first string be a “mask”
- Initialize: res_ind = 0 /* index to keep track of the processing of each character in i/p string */
ip_ind = 0 /* index to keep track of the processing of each character in the resultant string */ - Construct count array from mask_str. The count array would be:
(We can use a Boolean array here instead of an int count array because we don’t need a count, we need to know only if the character is present in a mask string)
count[‘a’] = 1
count[‘k’] = 1
count[‘m’] = 1
count[‘s’] = 1 - Process each character in the input string and if the count of that character is 0, then only add the character to the resultant string.
str = “tet tringng” // ’s’ has been removed because ’s’ was present in mask_str, but we have got two extra characters “ng”
ip_ind = 11
res_ind = 9
Put a ‘\0′ at the end of the string?
Implementations:
C++
// C++ program to remove duplicates, the order of// characters is not maintained in this progress#include <bits/stdc++.h>#define NO_OF_CHAR 256using namespace std;int* getcountarray(string str2){ int* count = (int*)calloc(sizeof(int), NO_OF_CHAR); for (int i = 0; i < str2.size(); i++) { count[str2[i]]++; } return count;}/* removeDirtyChars takes twostring as arguments: Firststring (str1) is the one fromwhere function removes dirtycharacters. Second string(str2)is the string which containall dirty characters which needto be removed from firststring */string removeDirtyChars(string str1, string str2){ // str2 is the string // which is to be removed int* count = getcountarray(str2); string res; // ip_idx helps to keep // track of the first string int ip_idx = 0; while (ip_idx < str1.size()) { char temp = str1[ip_idx]; if (count[temp] == 0) { res.push_back(temp); } ip_idx++; } return res;}// Driver Codeint main(){ string str1 = "geeksforgeeks"; string str2 = "mask"; // Function call cout << removeDirtyChars(str1, str2) << endl;} |
C
#include <stdio.h>#include <stdlib.h>#define NO_OF_CHARS 256/* Returns an array of size 256 containing count of characters in the passed char array */int* getCharCountArray(char* str){ int* count = (int*)calloc(sizeof(int), NO_OF_CHARS); int i; for (i = 0; *(str + i); i++) count[*(str + i)]++; return count;}/* removeDirtyChars takes twostring as arguments: Firststring (str) is the one fromwhere function removes dirtycharacters. Second string isthe string which contain alldirty characters which need tobe removed from first string*/char* removeDirtyChars(char* str, char* mask_str){ int* count = getCharCountArray(mask_str); int ip_ind = 0, res_ind = 0; while (*(str + ip_ind)) { char temp = *(str + ip_ind); if (count[temp] == 0) { *(str + res_ind) = *(str + ip_ind); res_ind++; } ip_ind++; } /* After above step string is ngring. Removing extra "iittg" after string*/ *(str + res_ind) = '\0'; return str;}/* Driver code*/int main(){ char str[] = "geeksforgeeks"; char mask_str[] = "mask"; printf("%s", removeDirtyChars(str, mask_str)); return 0;} |
Java
// Java program to remove duplicates, the order of// characters is not maintained in this programpublic class GFG { static final int NO_OF_CHARS = 256; /* Returns an array of size 256 containing count of characters in the passed char array */ static int[] getCharCountArray(String str) { int count[] = new int[NO_OF_CHARS]; for (int i = 0; i < str.length(); i++) count[str.charAt(i)]++; return count; } /* removeDirtyChars takes two string as arguments: First string (str) is the one from where function removes dirty characters. Second string is the string which contain all dirty characters which need to be removed from first string */ static String removeDirtyChars(String str, String mask_str) { int count[] = getCharCountArray(mask_str); int ip_ind = 0, res_ind = 0; char arr[] = str.toCharArray(); while (ip_ind != arr.length) { char temp = arr[ip_ind]; if (count[temp] == 0) { arr[res_ind] = arr[ip_ind]; res_ind++; } ip_ind++; } str = new String(arr); /* After above step string is ngring. Removing extra "iittg" after string*/ return str.substring(0, res_ind); } // Driver Code public static void main(String[] args) { String str = "geeksforgeeks"; String mask_str = "mask"; System.out.println(removeDirtyChars(str, mask_str)); }} |
Python
# Python program to remove characters# from first string which# are present in the second stringNO_OF_CHARS = 256# Utility function to convert# from string to listdef toList(string): temp = [] for x in string: temp.append(x) return temp# Utility function to# convert from list to stringdef toString(List): return ''.join(List)# Returns an array of size# 256 containing count of characters# in the passed char arraydef getCharCountArray(string): count = [0] * NO_OF_CHARS for i in string: count[ord(i)] += 1 return count# removeDirtyChars takes two# string as arguments: First# string (str) is the one# from where function removes dirty# characters. Second string# is the string which contain all# dirty characters which need# to be removed from first stringdef removeDirtyChars(string, mask_string): count = getCharCountArray(mask_string) ip_ind = 0 res_ind = 0 temp = '' str_list = toList(string) while ip_ind != len(str_list): temp = str_list[ip_ind] if count[ord(temp)] == 0: str_list[res_ind] = str_list[ip_ind] res_ind += 1 ip_ind += 1 # After above step string is ngring. # Removing extra "iittg" after string return toString(str_list[0:res_ind])# Driver codemask_string = "mask"string = "geeksforgeeks"print removeDirtyChars(string, mask_string)# This code is contributed by Bhavya Jain |
C#
// C# program to remove// duplicates, the order// of characters is not// maintained in this programusing System;class GFG { static int NO_OF_CHARS = 256; /* Returns an array of size 256 containing count of characters in the passed char array */ static int[] getCharCountArray(String str) { int[] count = new int[NO_OF_CHARS]; for (int i = 0; i < str.Length; i++) count[str[i]]++; return count; } /* removeDirtyChars takes two string as arguments: First string (str) is the one from where function removes dirty characters. Second string is the string which contain all dirty characters which need to be removed from first string */ static String removeDirtyChars(String str, String mask_str) { int[] count = getCharCountArray(mask_str); int ip_ind = 0, res_ind = 0; char[] arr = str.ToCharArray(); while (ip_ind != arr.Length) { char temp = arr[ip_ind]; if (count[temp] == 0) { arr[res_ind] = arr[ip_ind]; res_ind++; } ip_ind++; } str = new String(arr); /* After above step string is ngring. Removing extra "iittg" after string*/ return str.Substring(0, res_ind); } // Driver Code public static void Main() { String str = "geeksforgeeks"; String mask_str = "mask"; Console.WriteLine(removeDirtyChars(str, mask_str)); }}// This code is contributed by mits |
Javascript
<script>//Javascript Implementationlet NO_OF_CHARS = 256;function getcountarray(str2){ var count = new Array(NO_OF_CHARS).fill(0); for (var i = 0; i < str2.length; i++) { count[str2.charCodeAt(i)]++; } return count;} /* removeDirtyChars takes twostring as arguments: Firststring (str1) is the one fromwhere function removes dirtycharacters. Second string(str2)is the string which containall dirty characters which needto be removed from firststring */function removeDirtyChars(str1, str2){ // str2 is the string // which is to be removed var count = getcountarray(str2); var res =""; // ip_idx helps to keep // track of the first string var ip_idx = 0; while (ip_idx < str1.length) { var temp = str1[ip_idx]; if (count[temp.charCodeAt(0)] == 0) { res = res.concat(temp); } ip_idx++; } return res;} // Driver Codevar mask_string = "mask"var string = "geeksforgeeks"document.write(removeDirtyChars(string, mask_string)); // This code is contributed by shivani</script> |
geeforgee
Time Complexity: O(m+n) Where m is the length of the mask string and n is the length of the input string.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.



