Minimum operations required to convert all characters of a String to a given Character
Given 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 that can be converted is 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++
#include <bits/stdc++.h>
using namespace std;
void countOperations( int n, int k)
{
int div = 2 * k + 1;
if (n / 2 <= k) {
cout << 1 << "\n" ;
if (n > k)
cout << k + 1;
else
cout << n;
}
else {
if (n % div == 0) {
int oprn = n / div ;
cout << oprn << "\n" ;
int pos = k + 1;
cout << pos << " " ;
for ( int i = 1; i <= oprn; i++) {
cout << pos << " " ;
pos += div ;
}
}
else {
int oprn = n / div + 1;
cout << oprn << "\n" ;
int pos = n % div ;
if (n % div > k)
pos -= k;
for ( int i = 1; i <= oprn; i++) {
cout << pos << " " ;
pos += div ;
}
}
}
}
int main()
{
string str = "edfreqwsazxet" ;
char ch = '$' ;
int n = str.size();
int k = 4;
countOperations(n, k);
return 0;
}
|
Java
class GFG{
static void countOperations( int n, int k)
{
int div = 2 * k + 1 ;
if (n / 2 <= k)
{
System.out.print( 1 + "\n" );
if (n > k)
System.out.print(k + 1 );
else
System.out.print(n);
}
else
{
if (n % div == 0 )
{
int oprn = n / div;
System.out.print(oprn + "\n" );
int pos = k + 1 ;
System.out.print(pos + " " );
for ( int i = 1 ; i <= oprn; i++)
{
System.out.print(pos + " " );
pos += div;
}
}
else
{
int oprn = n / div + 1 ;
System.out.print(oprn + "\n" );
int pos = n % div;
if (n % div > k)
pos -= k;
for ( int i = 1 ; i <= oprn; i++)
{
System.out.print(pos + " " );
pos += div;
}
}
}
}
public static void main(String[] args)
{
String str = "edfreqwsazxet" ;
char ch = '$' ;
int n = str.length();
int k = 4 ;
countOperations(n, k);
}
}
|
Python3
def countOperations(n, k):
div = 2 * k + 1
if (n / / 2 < = k):
print ( 1 )
if (n > k):
print (k + 1 )
else :
print (n)
else :
if (n % div = = 0 ):
oprn = n / / div
print (oprn)
pos = k + 1
print (pos, end = " " )
for i in range ( 1 , oprn + 1 ):
print (pos, end = " " )
pos + = div
else :
oprn = n / / div + 1
print (oprn)
pos = n % div
if (n % div > k):
pos - = k
for i in range ( 1 , oprn + 1 ):
print (pos, end = " " )
pos + = div
if __name__ = = '__main__' :
str = "edfreqwsazxet"
ch = '$'
n = len ( str )
k = 4
countOperations(n, k)
|
C#
using System;
class GFG{
static void countOperations( int n, int k)
{
int div = 2 * k + 1;
if (n / 2 <= k)
{
Console.Write(1 + "\n" );
if (n > k)
Console.Write(k + 1);
else
Console.Write(n);
}
else
{
if (n % div == 0)
{
int oprn = n / div;
Console.Write(oprn + "\n" );
int pos = k + 1;
Console.Write(pos + " " );
for ( int i = 1; i <= oprn; i++)
{
Console.Write(pos + " " );
pos += div;
}
}
else
{
int oprn = n / div + 1;
Console.Write(oprn + "\n" );
int pos = n % div;
if (n % div > k)
pos -= k;
for ( int i = 1; i <= oprn; i++)
{
Console.Write(pos + " " );
pos += div;
}
}
}
}
public static void Main(String[] args)
{
String str = "edfreqwsazxet" ;
int n = str.Length;
int k = 4;
countOperations(n, k);
}
}
|
Javascript
<script>
function countOperations(n , k) {
var div = 2 * k + 1;
if (n / 2 <= k) {
document.write(1 + "<br/>" );
if (n > k)
document.write(k + 1);
else
document.write(n);
}
else {
if (n % div == 0) {
var oprn = parseInt(n / div);
document.write(oprn + "<br/>" );
var pos = k + 1;
document.write(pos + " " );
for (i = 1; i <= oprn; i++) {
document.write(pos + " " );
pos += div;
}
}
else {
var oprn = parseInt(n / div + 1);
document.write(oprn + "<br/>" );
var pos = n % div;
if (n % div > k)
pos -= k;
for (i = 1; i <= oprn; i++) {
document.write(pos + " " );
pos += div;
}
}
}
}
var str = "edfreqwsazxet" ;
var ch = '$' ;
var n = str.length;
var k = 4;
countOperations(n, k);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
27 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...