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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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 all characters other than alphabets from string
- Remove even frequency characters from the string
- Remove odd frequency characters from the string
- Remove all non-alphabetical characters of a String in Java
- Remove characters from a String that appears exactly K times
- Remove characters from string that appears strictly less than K times
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Remove minimum characters from string to split it into three substrings under given constraints
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- 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
- 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
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Transform string str1 into str2 by taking characters from string str3
- Minimum deletions from string to reduce it to string with at most 2 unique characters
Improved By : Mithun Kumar