给定字符串str 、字符ch和整数K ,任务是找到将字符串str 的所有字符转换为ch所需的最少操作次数。每个操作都涉及从索引i 的每一侧转换K 个最接近的字符,即索引[i – K, i + K]中的字符可以转换为ch 。
注意:每个索引只能是单个操作的一部分。
例子:
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 “$$$$$$$$$$$$”.
方法:
请按照以下步骤解决问题:
- 对于任何索引i ,可以转换的最大字符数为2 * K + 1 。因此,如果字符串中的字符总数不超过2 * K ,则只需一次操作即可将整个字符串转换为ch 。
- 否则,所需的操作次数将为ceil(n / (2*k+1)) 。
- 迭代字符串,从可用于操作的最左边的可能索引开始,并在它之后打印每个(2*k+1) th索引。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
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
Javascript
2
4 13
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live