📌  相关文章
📜  将给定字符串转换为长度为K的相等子字符串的串联所需的最小翻转

📅  最后修改于: 2021-04-29 12:48:43             🧑  作者: Mango

给定一个二进制字符串S和一个整数K ,任务是找到将给定字符串转换为K个长度相等的子字符串的串联所需的最小翻转次数。可以将给定的字符串拆分为K个长度的子字符串

例子:

方法:
可以使用贪婪方法解决该问题。
请按照以下步骤操作:

  • 从每个索引中以K个索引为增量对给定的字符串进行迭代,并保持0 s和1 s的计数。
  • 出现最少次数的字符必须翻转并保持递增计数
  • 对从0K-1的所有索引执行上述步骤,以获得所需的最少翻转次数。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
  
// Function that returns the minimum
// number of flips to convert
// the s into a concatenation
// of K-length sub-string
int minOperations(string S, int K)
{
    // Stores the result
    int ans = 0;
  
    // Iterate through string index
    for (int i = 0; i < K; i++) {
  
        // Stores count of 0s & 1s
        int zero = 0, one = 0;
  
        // Iterate making K jumps
        for (int j = i;
             j < S.size(); j += K) {
  
            // Count 0's
            if (S[j] == '0')
                zero++;
  
            // Count 1's
            else
                one++;
        }
  
        // Add minimum flips
        // for index i
        ans += min(zero, one);
    }
  
    // Return minimum number
    // of flips
    return ans;
}
  
// Driver Code
int main()
{
    string S = "110100101";
  
    int K = 3;
  
    cout << minOperations(S, K);
  
    return 0;
}


Java
// Java program to implement 
// the above approach 
import java.io.*;
  
class GFG{
  
// Function that returns the minimum
// number of flips to convert
// the s into a concatenation
// of K-length sub-string
public static int minOperations(String S, int K)
{
      
    // Stores the result
    int ans = 0;
  
    // Iterate through string index
    for(int i = 0; i < K; i++)
    {
  
        // Stores count of 0s & 1s
        int zero = 0, one = 0;
  
        // Iterate making K jumps
        for(int j = i; j < S.length(); j += K)
        {
              
            // Count 0's
            if (S.charAt(j) == '0')
                zero++;
  
            // Count 1's
            else
                one++;
        }
  
        // Add minimum flips
        // for index i
        ans += Math.min(zero, one);
    }
  
    // Return minimum number
    // of flips
    return ans;
}
  
// Driver Code
public static void main(String args[])
{
    String S = "110100101";
  
    int K = 3;
  
    System.out.println(minOperations(S, K));
}
}
  
// This code is contributed by grand_master


Python3
# Python3 program to implement
# the above approach
  
# Function that returns the minimum
# number of flips to convert the s
# into a concatenation of K-length
# sub-string
def minOperations(S, K):
  
    # Stores the result
    ans = 0
  
    # Iterate through string index
    for i in range(K):
  
        # Stores count of 0s & 1s
        zero, one = 0, 0
  
        # Iterate making K jumps
        for j in range(i, len(S), K):
  
            # Count 0's
            if(S[j] == '0'):
                zero += 1
  
            # Count 1's
            else:
                one += 1
  
        # Add minimum flips
        # for index i
        ans += min(zero, one)
  
    # Return minimum number
    # of flips 
    return ans
  
# Driver code
if __name__ == '__main__':
  
    s = "110100101"
    K = 3
  
    print(minOperations(s, K))
  
# This code is contributed by Shivam Singh


C#
// C# program to implement 
// the above approach 
using System;
  
class GFG{
  
// Function that returns the minimum
// number of flips to convert
// the s into a concatenation
// of K-length sub-string
public static int minOperations(String S, int K)
{
      
    // Stores the result
    int ans = 0;
  
    // Iterate through string index
    for(int i = 0; i < K; i++)
    {
  
        // Stores count of 0s & 1s
        int zero = 0, one = 0;
  
        // Iterate making K jumps
        for(int j = i; j < S.Length; j += K)
        {
              
            // Count 0's
            if (S[j] == '0')
                zero++;
  
            // Count 1's
            else
                one++;
        }
  
        // Add minimum flips
        // for index i
        ans += Math.Min(zero, one);
    }
  
    // Return minimum number
    // of flips
    return ans;
}
  
// Driver Code
public static void Main(String []args)
{
    String S = "110100101";
  
    int K = 3;
  
    Console.WriteLine(minOperations(S, K));
}
}
  
// This code is contributed by 29AjayKumar


输出:
2

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