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.
Examples:
Input: str = “abcsdx”, K = 2, ch = ‘#’
Output: 2
Explanation:
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 = ‘$’
Output:2
Explanation:
Operation 1: Select i = 2, therefore, str = “Hellomypkfsg” modifies to “$$$$$mypkfsg”.
Operation 2: Select i = 9, therefore, str = “$$$$$mypkfsg” modifies to “$$$$$$$$$$$$”.
Approach:
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:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find the minimum // number of operations required void countOperations(int n, int k) { // Maximum number of characters that // can be changed in one operation int div = 2 * k + 1; // If length of the string less than // maximum number of characters that // can be changed in an operation if (n / 2 <= k) { cout << 1 << "\n"; // Set the last index as the // index for the operation if (n > k) cout << k + 1; else cout << n; } // Otherwise else { // If size of the string is // equal to the maximum number // of characters in an operation if (n % div == 0) { // Find the number of // operations required int oprn = n / div; cout << oprn << "\n"; // Find the starting postion int pos = k + 1; cout << pos << " "; for (int i = 1; i <= oprn; i++) { // Print i-th index cout << pos << " "; // Shift to next index pos += div; } } // Otherwise else { // Find the number of // operations required int oprn = n / div + 1; cout << oprn << "\n"; int pos = n % div; // If n % div exceeds k if (n % div > k) pos -= k; for (int i = 1; i <= oprn; i++) { // Print i-th index cout << pos << " "; // Shift to next index pos += div; } } } } // Driver Code int main() { string str = "edfreqwsazxet"; char ch = '$'; int n = str.size(); int k = 4; countOperations(n, k); return 0; } |
Java
// Java program to implement // the above approach class GFG{ // Function to find the minimum // number of operations required static void countOperations(int n, int k) { // Maximum number of characters that // can be changed in one operation int div = 2 * k + 1; // If length of the String less than // maximum number of characters that // can be changed in an operation if (n / 2 <= k) { System.out.print(1 + "\n"); // Set the last index as the // index for the operation if (n > k) System.out.print(k + 1); else System.out.print(n); } // Otherwise else { // If size of the String is // equal to the maximum number // of characters in an operation if (n % div == 0) { // Find the number of // operations required int oprn = n / div; System.out.print(oprn + "\n"); // Find the starting postion int pos = k + 1; System.out.print(pos + " "); for(int i = 1; i <= oprn; i++) { // Print i-th index System.out.print(pos + " "); // Shift to next index pos += div; } } // Otherwise else { // Find the number of // operations required int oprn = n / div + 1; System.out.print(oprn + "\n"); int pos = n % div; // If n % div exceeds k if (n % div > k) pos -= k; for(int i = 1; i <= oprn; i++) { // Print i-th index System.out.print(pos + " "); // Shift to next index pos += div; } } } } // Driver Code public static void main(String[] args) { String str = "edfreqwsazxet"; char ch = '$'; int n = str.length(); int k = 4; countOperations(n, k); } } // This code is contributed by amal kumar choubey |
Python3
# Python3 program to implement # the above approach # Function to find the minimum # number of operations required def countOperations(n, k): # Maximum number of characters that # can be changed in one operation div = 2 * k + 1 # If length of the less than # maximum number of characters that # can be changed in an operation if (n // 2 <= k): print(1) # Set the last index as the # index for the operation if (n > k): print(k + 1) else: print(n) # Otherwise else: # If size of the is # equal to the maximum number # of characters in an operation if (n % div == 0): # Find the number of # operations required oprn = n // div print(oprn) # Find the starting postion pos = k + 1 print(pos, end = " ") for i in range(1, oprn + 1): # Print i-th index print(pos, end = " ") # Shift to next index pos += div # Otherwise else: # Find the number of # operations required oprn = n // div + 1 print(oprn) pos = n % div # If n % div exceeds k if (n % div > k): pos -= k for i in range(1, oprn + 1): # Print i-th index print(pos, end = " ") # Shift to next index pos += div # Driver Code if __name__ == '__main__': str = "edfreqwsazxet" ch = '$' n = len(str) k = 4 countOperations(n, k) # This code is contributed by mohit kumar 29 |
C#
// C# program to implement // the above approach using System; class GFG{ // Function to find the minimum // number of operations required static void countOperations(int n, int k) { // Maximum number of characters that // can be changed in one operation int div = 2 * k + 1; // If length of the String less than // maximum number of characters that // can be changed in an operation if (n / 2 <= k) { Console.Write(1 + "\n"); // Set the last index as the // index for the operation if (n > k) Console.Write(k + 1); else Console.Write(n); } // Otherwise else { // If size of the String is // equal to the maximum number // of characters in an operation if (n % div == 0) { // Find the number of // operations required int oprn = n / div; Console.Write(oprn + "\n"); // Find the starting postion int pos = k + 1; Console.Write(pos + " "); for(int i = 1; i <= oprn; i++) { // Print i-th index Console.Write(pos + " "); // Shift to next index pos += div; } } // Otherwise else { // Find the number of // operations required int oprn = n / div + 1; Console.Write(oprn + "\n"); int pos = n % div; // If n % div exceeds k if (n % div > k) pos -= k; for(int i = 1; i <= oprn; i++) { // Print i-th index Console.Write(pos + " "); // Shift to next index pos += div; } } } } // Driver Code public static void Main(String[] args) { String str = "edfreqwsazxet"; int n = str.Length; int k = 4; countOperations(n, k); } } // This code is contributed by Rohit_ranjan |
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.
Recommended Posts:
- 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 contribute@geeksforgeeks.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.

