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.
- Program to print characters present at prime indexes in a given string
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Remove even frequency characters from the string
- Remove all characters other than alphabets from string
- Remove all non-alphabetical characters of a String in Java
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Remove characters from string that appears strictly less than K times
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find the character in first string that is present at minimum index in second string
- Most frequent word in first String which is not present in second String
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Minimal moves to form a string by adding characters or appending string itself
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Minimum deletions from string to reduce it to string with at most 2 unique characters
Improved By : Mithun Kumar