📌  相关文章
📜  给定二进制字符串可被2整除的子序列数

📅  最后修改于: 2021-05-06 18:23:12             🧑  作者: Mango

给定长度为N的二进制字符串str ,任务是找到被2整除的str子序列的计数。子序列中的前导零是允许的。

例子:

天真的方法:天真的方法是生成所有可能的子序列,并检查它们是否可被2整除。其时间复杂度为O(2 N * N)。

高效的方法:可以观察到,只有二进制数字以0结束时,它才能被2整除。现在,任务是只计算以0结尾的子序列的数量。所以,对于每一个索引i,使得STR [1] =“0”,在找到结束子序列的数目。该值等于2 i (基于0的索引)。因此,对于所有i ,最终答案将等于2 i的总和,从而str [i] =’0′

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the count
// of the required subsequences
int countSubSeq(string str, int len)
{
    // To store the final answer
    int ans = 0;
  
    // Multiplier
    int mul = 1;
  
    // Loop to find the answer
    for (int i = 0; i < len; i++) {
  
        // Condition to update the answer
        if (str[i] == '0')
            ans += mul;
        // updating multiplier
        mul *= 2;
    }
  
    return ans;
}
  
// Driver code
int main()
{
    string str = "10010";
    int len = str.length();
  
    cout << countSubSeq(str, len);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
  
// Function to return the count
// of the required subsequences
static int countSubSeq(String str, int len)
{
    // To store the final answer
    int ans = 0;
  
    // Multiplier
    int mul = 1;
  
    // Loop to find the answer
    for (int i = 0; i < len; i++) 
    {
  
        // Condition to update the answer
        if (str.charAt(i) == '0')
            ans += mul;
              
        // updating multiplier
        mul *= 2;
    }
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    String str = "10010";
    int len = str.length();
  
    System.out.print(countSubSeq(str, len));
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
  
# Function to return the count
# of the required subsequences
def countSubSeq(strr, lenn):
      
    # To store the final answer
    ans = 0
  
    # Multiplier
    mul = 1
  
    # Loop to find the answer
    for i in range(lenn):
  
        # Condition to update the answer
        if (strr[i] == '0'):
            ans += mul
              
        # updating multiplier
        mul *= 2
  
    return ans
  
# Driver code
strr = "10010"
lenn = len(strr)
  
print(countSubSeq(strr, lenn))
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach 
using System;
  
class GFG
{
      
    // Function to return the count 
    // of the required subsequences 
    static int countSubSeq(string str, int len) 
    { 
        // To store the final answer 
        int ans = 0; 
      
        // Multiplier 
        int mul = 1; 
      
        // Loop to find the answer 
        for (int i = 0; i < len; i++) 
        { 
      
            // Condition to update the answer 
            if (str[i] == '0') 
                ans += mul; 
                  
            // updating multiplier 
            mul *= 2; 
        } 
        return ans; 
    } 
      
    // Driver code 
    static public void Main ()
    { 
        string str = "10010"; 
        int len = str.Length; 
      
        Console.WriteLine(countSubSeq(str, len)); 
    } 
}
  
// This code is contributed by AnkitRai01


输出:

22