Given a string W, change the string in such a way that it does not contain any of the “forbidden” strings S1 to Sn as one of its substrings. The rules that govern this change are as follows:
1. Case of the characters does not matter i.e “XyZ” is same as “xYZ”.
2. Change all the characters that are covered by the substrings in the original string W such that a particular letter lt occurs maximum number of times in the changed string and the changed string is lexicographically the smallest string that can be obtained from all possible combinations.
3. Characters that are not within the forbidden substrings are not allowed to change.
4. The case of the changed character must be the same as the case of the original character at that position in string W.
5. If the letter lt is part of the substring then it must be changed to some other character according to above rules.
Input : n = 3 s1 = "etr" s2 = "ed" s3 = "ied" W = "PEtrUnited" letter = "d" Output : PDddUnitda Input : n = 1 s1 = "PetrsDreamOh" W = "PetrsDreamOh" letter = h Output : HhhhhHhhhhHa
Example 1: First character P does not belong to any of the substrings therefore it is not changed. The next three characters form the substring “etr” and are changed to “Ddd”. The next four characters do not belong to any of the forbidden substrings and remain unchanged. The next two characters form the substring “ed” and are changed to “da” since d is The last character itself, it is replaced with another character ‘a’ such that the string is lexicographically the smallest.
Notice: “Etr” = “etr” and the changed substring “Ddd” has first character as ‘D’ since first letter of “Etr” is in uppercase.
Example 2: Since the entire string is a forbidden string, it is replaced with letter ‘h’ from first to second last character according to the case. The last character is ‘h’ therefore it is replaced with letter ‘a’ such that the string is lexicographically the smallest.
Check for every character of the string W, if it is the beginning of the any of the substrings or not. Use another array to keep record of the characters of W that are part of the forbidden strings and needs to be changed.
The characters are changed according to the following rules:
1. If W[i] = letter and letter = ‘a’ then W[i] = ‘b’
2. If W[i] = letter and letter !=’a’ then W[i] = ‘a’
3. If W[i] != letter then W[i] = letter
The first and second condition will also be used when W[i] is an uppercase character.
Below is the implementation of above approach:
- Remove minimum number of characters so that two strings become anagram
- Pairs of complete strings in two sets of strings
- Search in an array of strings where non-empty strings are sorted
- Meta Strings (Check if two strings can become same after a swap in one string)
- Number of common base strings for two strings
- Count of strings that become equal to one of the two strings after one removal
- Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array
- Distinct strings such that they contains given strings as sub-sequences
- Count of same length Strings that exists lexicographically in between two given Strings
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Print all Strings from array A having all strings from array B as subsequence
- Search strings with the help of given pattern in an Array of strings
- Print all strings of maximum length from an array of strings
- Remove characters from the first string which are present in the second string
- Remove duplicates from a given string
- Remove "b" and "ac" from a given string
- Recursively remove all adjacent duplicates
- Remove spaces from a given string
- Remove extra spaces from a string
- Remove a character from a string to make it a palindrome
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.