VOOZH about

URL: https://www.geeksforgeeks.org/dsa/remove-characters-from-the-first-string-which-are-present-in-the-second-string/

⇱ Remove characters from the first string which are present in the second string - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 13 Jul, 2023

Given two strings string1 and string2, remove those characters from the first string(string1) which are present in the second string(string2). Both strings are different and contain only lowercase characters.
NOTE: The size of the first string is always greater than the size of the second string( |string1| > |string2|).

Example:

Input:
string1 = "computer"
string2 = "cat"
Output: "ompuer"
Explanation: After removing characters(c, a, t)
from string1 we get "ompuer".

Input:
string1 = "occurrence"
string2 = "car"
Output: "ouene"
Explanation: After removing characters
(c, a, r) from string1 we get "ouene".

We strongly recommend that you click here and practise 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”

  1. 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 */
  2. 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
  3. 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:


Output
geeforgee

Time Complexity: O(m+n) Where m is the length of the mask string and n is the length of the input string. 
Auxiliary Space: O(m)

An efficient solution is we find every character of string2 in string1 if that character is present then we simply erase that character from string1.


Output
geeforgee

Time Complexity: O(n*m), where n is the size of given string2 and m is the size of string1.
Auxiliary Space: O(1), as no extra space is used

Efficient Solution: An efficient solution is that we can mark the occurrence of all characters present in second string by -1 in frequency character array and then while traversing first string we can ignore the marked characters as shown in below program. 


Output
geeforgee

Time Complexity: O(|S1|), where |S1| is the size of given string 1.
Auxiliary Space: O(1), as only an array of constant size (26) is used. 

Comment
Article Tags: