We strongly recommend that you click here and practice it, before moving on to the solution.
Algorithm: Let first input string be”test string” and the string which has characters to be removed from first string be “mask”
res_ind = 0 /* index to keep track of processing of each character in i/p string */
ip_ind = 0 /* index to keep track of processing of each character in the resultant string */
2: Construct count array from mask_str. Count array would be:
(We can use Boolean array here instead of int count array because we don’t need count, we need to know only if character is present in mask string)
count[‘a’] = 1
count[‘k’] = 1
count[‘m’] = 1
count[‘s’] = 1
3: Process each character of the input string and if 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 we have got two extra characters “ng”
ip_ind = 11
res_ind = 9
4: Put a ‘\0′ at the end of the string?
Time Complexity: O(m+n) Where m is the length of 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.
- Most frequent word in first String which is not present in second String
- Find elements which are present in first array and not in second
- Find the character in first string that is present at minimum index in second string
- Count of times second string can be formed from the characters of first string
- Check whether second string can be formed from characters of first string
- Count subsequences in first string which are anagrams of the second string
- Count of strings in the first array which are smaller than every string in the second array
- Find the sum of the ascii values of characters which are present at prime positions
- Count number of rotated strings which have more number of vowels in the first half than second half
- Minimum cost to remove the spaces between characters of a String by rearranging the characters
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Minimum changes required to make first string substring of second string
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Program to print characters present at prime indexes in a given string
- Remove first adjacent pairs of similar characters until possible
- Find repeated character present first in a string
- First string from the given array whose reverse is also present in the same array
- Given two strings, find if first string is a subsequence of second
- Longest Even Length Substring such that Sum of First and Second Half is same
Improved By : Mithun Kumar