给定大小为N的二进制字符串S和整数K。任务是找到出现在大小为K的子字符串中的最大置位位数。
例子:
Input: S = “100111010”, K = 3
Output: 3
Explanation:
The substring “111” contains 3 set bits.
Input:S = “0000000”, K = 4
Output: 0
Explanation: S doesn’t have any set bits in it, so ans is 0.
天真的方法:
- 生成所有大小为K的子字符串。
- 在所有子串中找到最大置位数。
时间复杂度: O(N 2 )。
辅助空间: O(1)。
高效的方法:可以使用滑动窗口技术解决该问题。
- 以maxcount变量存储设置位的最大计数,以Count变量存储当前窗口的计数设置位。
- 从1横移字符串至K并计算组位的计数和存储作为MAXCOUNT。
- 从K +1遍历字符串到字符串的长度。
- 在每次迭代中,如果设置了第(K – i)位,则减少计数。如果设置了第i个位,则增加计数。比较并更新maxcount 。
- 完成数组遍历后,最后返回maxcount。
下面是上述方法的实现:
C++
// C++ program to find the maximum
// set bits in a substring of size K
#include
using namespace std;
// Function that find Maximum number
// of set bit appears in a substring
// of size K.
int maxSetBitCount(string s, int k)
{
int maxCount = 0, n = s.length();
int count = 0;
// Traverse string 1 to k
for(int i = 0; i < k; i++)
{
// Incerement count if
// character is set bit
if (s[i] == '1')
count++;
}
maxCount = count;
// Traverse string k+1
// to length of string
for(int i = k; i < n; i++)
{
// Remove the contribution of the
// (i - k)th character which is no
// longer in the window
if (s[i - k] == '1')
count--;
// Add the contribution of
// the current character
if (s[i] == '1')
count++;
// Update maxCount at for
// each window of size k
maxCount = max(maxCount, count);
}
// Return maxCount
return maxCount;
}
// Driver code
int main()
{
string s = "100111010";
int k = 3;
cout << (maxSetBitCount(s, k));
return 0;
}
// This code is contributed by Rajput-Ji
Java
// Java program to find the maximum
// set bits in a substring of size K
import java.util.*;
class GFG {
// Function that find Maximum number
// of set bit appears in a substring
// of size K.
static int maxSetBitCount(String s, int k)
{
int maxCount = 0, n = s.length();
int count = 0;
// Traverse string 1 to k
for (int i = 0; i < k; i++) {
// Incerement count if
// character is set bit
if (s.charAt(i) == '1')
count++;
}
maxCount = count;
// Traverse string k+1
// to length of string
for (int i = k; i < n; i++) {
// remove the contribution of the
// (i - k)th character which is no
// longer in the window
if (s.charAt(i - k) == '1')
count--;
// add the contribution of
// the current character
if (s.charAt(i) == '1')
count++;
// update maxCount at for
// each window of size k
maxCount = Math.max(maxCount, count);
}
// return maxCount
return maxCount;
}
// Driver Program
public static void main(String[] args)
{
String s = "100111010";
int k = 3;
System.out.println(maxSetBitCount(s, k));
}
}
Python3
# Python3 program to find the maximum
# set bits in a substring of size K
# Function that find Maximum number
# of set bit appears in a substring
# of size K.
def maxSetBitCount(s, k):
maxCount = 0
n = len(s)
count = 0
# Traverse string 1 to k
for i in range(k):
# Incerement count if
# character is set bit
if (s[i] == '1'):
count += 1
maxCount = count
# Traverse string k+1
# to length of string
for i in range(k, n):
# Remove the contribution of the
# (i - k)th character which is no
# longer in the window
if (s[i - k] == '1'):
count -= 1
# Add the contribution of
# the current character
if (s[i] == '1'):
count += 1
# Update maxCount at for
# each window of size k
maxCount = max(maxCount, count)
# Return maxCount
return maxCount
# Driver code
if __name__ == '__main__':
s = "100111010"
k = 3
print(maxSetBitCount(s, k))
# This code is contributed by mohit kumar 29
C#
// C# program to find the maximum
// set bits in a substring of size K
using System;
class GFG {
// Function that find Maximum number
// of set bit appears in a substring
// of size K.
static int maxSetBitCount(string s, int k)
{
int maxCount = 0, n = s.Length;
int count = 0;
// Traverse string 1 to k
for (int i = 0; i < k; i++)
{
// Incerement count if
// character is set bit
if (s[i] == '1')
count++;
}
maxCount = count;
// Traverse string k+1
// to length of string
for (int i = k; i < n; i++)
{
// remove the contribution of the
// (i - k)th character which is no
// longer in the window
if (s[i - k] == '1')
count--;
// add the contribution of
// the current character
if (s[i] == '1')
count++;
// update maxCount at for
// each window of size k
maxCount = Math.Max(maxCount, count);
}
// return maxCount
return maxCount;
}
// Driver Program
public static void Main()
{
string s = "100111010";
int k = 3;
Console.Write(maxSetBitCount(s, k));
}
}
// This code is contributed by Code_Mech
输出:
3
时间复杂度: O(N)。
辅助空间: O(1)。