Given a string str, a character ch and an integer K, the task is to find the minimum number of operations required to convert all the characters of string str to ch. Each operation involves converting K closest characters from each side of the index i, i.e., characters in the indices [i – K, i + K] can be converted to ch.
Note: Each index can be part of a single operation only.
Input: str = “abcsdx”, K = 2, ch = ‘#’
Operation 1: Select i = 1, therefore, str = “abcsdx” modifies to “###sdx”.
Operation 2: Select i = 6, therefore, str = “###sdx” modifies to “######”.
Input: str = “Hellomypkfsg”, k = 3, ch = ‘$’
Operation 1: Select i = 2, therefore, str = “Hellomypkfsg” modifies to “$$$$$mypkfsg”.
Operation 2: Select i = 9, therefore, str = “$$$$$mypkfsg” modifies to “$$$$$$$$$$$$”.
Follow the steps below to solve the problem:
- For any index i, the maximum number of characters than can be converted are 2 * K + 1. Therefore, if the total number of characters in the string does not exceed 2 * K, a single operation is required only to convert the entire string into ch.
- Otherwise, the number of operations required will be ceil(n / (2*k+1)).
- Iterate over the string, starting from the leftmost possible index that can be used for the operation and print every (2*k+1)th index after it .
Below is the implementation of the above approach:
2 4 13
Time Complexity: O(N)
Auxiliary Space: O(1)
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.
- Minimum given operations required to convert a given binary string to all 1's
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum number of given operations required to convert a string to another string
- Minimum number of operations to move all uppercase characters before all lower case characters
- Minimum cost of flipping characters required to convert Binary String to 0s only
- Minimum operations required to convert X to Y by multiplying X with the given co-primes
- Count minimum character replacements required such that given string satisfies the given conditions
- Minimum characters required to be removed to make frequency of each character unique
- Minimum number operations required to convert n to m | Set-2
- Minimum operations required to make the string satisfy the given condition
- Minimum number of operations required to obtain a given Binary String
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Replace all occurrences of character X with character Y in given string
- Minimum removal of characters required such that permutation of given string is a palindrome
- Minimum number of Appends of X or Y characters from the end to the front required to obtain given string
- Minimum number of steps required to obtain the given Array by the given operations
- Minimum sum of squares of character counts in a given string after removing k characters
- Minimum operations to transform given string to another by moving characters to front or end
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Minimum substring flips required to convert given binary string to another
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.