给定一个二进制字符串S和一个整数K ,任务是找到将给定字符串转换为K 长度相等子字符串的串联所需的最小翻转次数。给定的字符串可以拆分为K 个长度的子字符串。
例子:
Input: S = “101100101”, K = 3
Output: 1
Explanation:
Flip the ‘0’ from index 5 to ‘1’.
The resultant string is S = “101101101”.
It is the concatenation of substring “101”.
Hence, the minimum number of flips required is 1.
Input: S = “10110111”, K = 4
Output: 2
Explanation:
Flip the ‘0’ and ‘1’ at indexes 4 and 5 respectively.
The resultant string is S = “10111011”.
It is the concatenation of the substring “1011”.
Hence, the minimum number of flips required is 2.
方法:
这个问题可以用贪心方法解决。
请按照以下步骤操作:
- 使用每个索引的K 个索引的增量迭代给定的字符串,并保留0和1的计数。
- 出现最少次数的字符必须翻转并继续增加计数。
- 对从0到K-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
Javascript
2
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live