📌  相关文章
📜  给定二进制字符串的所有 K 长度子字符串的按位或中的 setbit 计数

📅  最后修改于: 2022-05-13 01:56:10.316000             🧑  作者: Mango

给定二进制字符串的所有 K 长度子字符串的按位或中的 setbit 计数

给定一个长度为N的二进制字符串str ,任务是在字符串str的所有K个长度子串的按位或中找到 setbit 的数量。

例子:

方法:问题的解决是基于滑动窗口技术的概念。请按照以下步骤操作:

  • 使用滑动窗口技术并找到所有大小为K的子串。
  • 存储所有大小为K的子字符串。(这里使用字符串向量)。
  • 对所有字符串做OR
  • 计算设置位的数量并返回。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to make OR two string
string ORing(string a, string b)
{
    string ans = "";
    int n = a.size();
    for (int i = 0; i < n; i++) {
        if (a[i] == '1' || b[i] == '1')
            ans += "1";
        else
            ans += "0";
    }
    return ans;
}
 
// Function to check the setbits
// in OR of all K size substring
int solve(string str, int N, int K)
{
    // Making vector to store answer
    vector v1;
    int windowsize = K;
    int i = 0;
    int j = 0;
    string temp = "";
 
    // Using sliding window technique
    while (j < N) {
        temp.push_back(str[j]);
        if (j - i + 1 < windowsize) {
            j++;
        }
        else {
            v1.push_back(temp);
            reverse(temp.begin(), temp.end());
            temp.pop_back();
            reverse(temp.begin(), temp.end());
            i++;
            j++;
        }
    }
 
    // OR of all strings which
    // are present in the vector
    string a = v1[0];
    for (int i = 1; i < v1.size(); i++) {
        a = ORing(a, v1[i]);
    }
 
    // Counting number of set bit
    int count = 0;
    for (int i = 0; i < a.size(); i++) {
        if (a[i] == '1') {
            count++;
        }
    }
    return count;
}
 
// Driver code
int main()
{
    int N = 4;
    int K = 3;
    string str = "1111";
 
    // Calling function
    cout << solve(str, N, K);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
 
  // Function to make OR two String
  static String ORing(String a, String b) {
    String ans = "";
    int n = a.length();
    for (int i = 0; i < n; i++) {
      if (a.charAt(i) == '1' || b.charAt(i) == '1')
        ans += "1";
      else
        ans += "0";
    }
    return ans;
  }
 
  static String reverse(String input) {
    char[] a = input.toCharArray();
    int l, r = a.length - 1;
    for (l = 0; l < r; l++, r--) {
      char temp = a[l];
      a[l] = a[r];
      a[r] = temp;
    }
    return String.valueOf(a);
  }
 
  // Function to check the setbits
  // in OR of all K size subString
  static int solve(String str, int N, int K)
  {
 
    // Making vector to store answer
    Vector v1 = new Vector<>();
    int windowsize = K;
    int i = 0;
    int j = 0;
    String temp = "";
 
    // Using sliding window technique
    while (j < N) {
      temp += (str.charAt(j));
      if (j - i + 1 < windowsize) {
        j++;
      } else {
        v1.add(temp);
        temp = reverse(temp);
        temp = temp.substring(0, temp.length() - 1);
        temp = reverse(temp);
        i++;
        j++;
      }
    }
 
    // OR of all Strings which
    // are present in the vector
    String a = v1.get(0);
    for (i = 1; i < v1.size(); i++) {
      a = ORing(a, v1.get(i));
    }
 
    // Counting number of set bit
    int count = 0;
    for (i = 0; i < a.length(); i++) {
      if (a.charAt(i) == '1') {
        count++;
      }
    }
    return count;
  }
 
  // Driver code
  public static void main(String[] args) {
    int N = 4;
    int K = 3;
    String str = "1111";
 
    // Calling function
    System.out.print(solve(str, N, K));
  }
}
 
// This code is contributed by Rajput-Ji


Python3
# Python code for the above approach
 
# Function to make OR two string
def ORing(a, b):
    ans = "";
    n = len(a)
    for i in range(n):
        if (a[i] == '1' or b[i] == '1'):
            ans += '1';
        else:
            ans += '0';
     
    return list(ans)
 
# Function to check the setbits
# in OR of all K size substring
def solve(str, N, K):
   
    # Making vector to store answer
    v1 = [];
    windowsize = K;
    i = 0;
    j = 0;
    temp = [];
 
    # Using sliding window technique
    while (j < N):
        temp.append(str[j]);
        if (j - i + 1 < windowsize):
            j += 1
        else:
            v1.append(''.join(temp));
            temp.pop(0)
            i += 1
            j += 1
         
    # OR of all strings which
    # are present in the vector
    a = v1[0];
    for i in range(1, len(v1)):
        a = ORing(a, v1[i]);
     
    # Counting number of set bit
    count = 0;
    for i in range(len(a)):
        if (a[i] == '1'):
            count = count + 1;
         
    return count;
 
# Driver code
N = 4;
 
K = 3;
str = "1111";
 
# Calling function
print(solve(str, N, K));
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG {
 
  // Function to make OR two String
  static String ORing(String a, String b) {
    String ans = "";
    int n = a.Length;
    for (int i = 0; i < n; i++) {
      if (a[i] == '1' || b[i] == '1')
        ans += "1";
      else
        ans += "0";
    }
    return ans;
  }
 
  static String reverse(String input) {
    char[] a = input.ToCharArray();
    int l, r = a.Length - 1;
    for (l = 0; l < r; l++, r--) {
      char temp = a[l];
      a[l] = a[r];
      a[r] = temp;
    }
    return String.Join("",a);
  }
 
  // Function to check the setbits
  // in OR of all K size subString
  static int solve(String str, int N, int K)
  {
 
    // Making vector to store answer
    List v1 = new List();
    int windowsize = K;
    int i = 0;
    int j = 0;
    String temp = "";
 
    // Using sliding window technique
    while (j < N) {
      temp += (str[j]);
      if (j - i + 1 < windowsize) {
        j++;
      } else {
        v1.Add(temp);
        temp = reverse(temp);
        temp = temp.Substring(0, temp.Length - 1);
        temp = reverse(temp);
        i++;
        j++;
      }
    }
 
    // OR of all Strings which
    // are present in the vector
    String a = v1[0];
    for (i = 1; i < v1.Count; i++) {
      a = ORing(a, v1[i]);
    }
 
    // Counting number of set bit
    int count = 0;
    for (i = 0; i < a.Length; i++) {
      if (a[i] == '1') {
        count++;
      }
    }
    return count;
  }
 
  // Driver code
  public static void Main(String[] args)
  {
    int N = 4;
    int K = 3;
    String str = "1111";
 
    // Calling function
    Console.Write(solve(str, N, K));
  }
}
 
// This code is contributed by 29AjayKumar


Javascript



输出
3

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