📌  相关文章
📜  将字符串的所有字符转换为给定字符所需的最少操作

📅  最后修改于: 2021-05-06 08:24:31             🧑  作者: Mango

给定一个字符串str ,一个字符ch和一个整数K ,任务是找到将字符串str的所有字符转换为ch所需的最少操作数。每个操作都涉及从索引i的每一侧转换K个最接近的字符,即,可以将索引[i – K,i + K]中的字符转换为ch
注意:每个索引只能是单个操作的一部分。
例子:

方法:
请按照以下步骤解决问题:

  1. 对于任何索引i ,可以转换的最大字符数为2 * K +1 。因此,如果字符串中的字符总数不超过2 * K ,则只需执行一次操作即可将整个字符串转换为ch
  2. 否则,所需的操作数将为ceil(n /(2 * k + 1))
  3. 从可用于该操作的最左边的索引开始,对字符串迭代,并在其后打印第(2 * k + 1)索引

下面是上述方法的实现:

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


输出:
2
4 13

时间复杂度: O(N)
辅助空间: O(1)