📌  相关文章
📜  二进制字符串的 K 大小子字符串中的最大设置位数计数

📅  最后修改于: 2021-09-06 05:50:53             🧑  作者: Mango

给定一个大小为N的二进制字符串S和一个整数K 。任务是找出大小为 K 的子串中出现的设置位的最大数目。

例子:

天真的方法:

  1. 生成所有大小为 K 的子串。
  2. 查找所有子串中设置位的最大计数。

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

有效的方法:该问题可以使用滑动窗口技术解决。

  1. maxcount变量存储设置位的最大计数和Count变量存储当前窗口的设置位计数。
  2. 遍历字符串从 1 到 K 并计算设置位的计数并存储为maxcount
  3. 横动字符串从K + 1到该字符串的长度。
  4. 在每次迭代中,如果设置了(K – i)位,则减少计数。如果设置了i位,则增加计数。比较和更新maxcount
  5. 完成数组遍历后,最终返回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++)
    {
         
       // Increment 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++) {
            // Increment 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):
         
        # Increment 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++)
    {
        // Increment 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


Javascript


输出:
3

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live