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 = “a_bcsdx” 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 = “He_llomypkfsg” modifies to “$$$$$mypkfsg”. _

Operation 2: Select i = 9, therefore, str = “$$$$$mypkfsg” modifies to “$$$$$$$$$$$$”.

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

Follow the steps below to solve the problem:

  1. 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.
  2. Otherwise, the number of operations required will be ceil(n / (2*k+1)).
  3. 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++
  • Java

// 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;

}

Output:

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.

#greedy #mathematical #strings #arrays

Minimum operations required to convert all characters of a String to a given Character
1.60 GEEK