📌  相关文章
📜  二进制字符串中具有奇十进制值的子字符串的数量

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

二进制字符串中具有奇十进制值的子字符串的数量

给定一个只包含 0 和 1 的二进制字符串。编写一个程序来查找该字符串的十进制表示为奇数的子字符串的数量。

例子 :

Input : 101
Output : 3
Explanation : Substrings with odd decimal 
              representation are:
              {1, 1, 101}

Input : 1101
Output : 6
Explanation : Substrings with odd decimal 
              representation are:
              {1, 1, 1, 11, 101, 1011}

蛮力方法:解决上述问题的最简单方法是生成给定字符串的所有可能子字符串并将它们转换为十进制并检查十进制表示是否为奇数。二进制到十进制的转换可以参考这篇文章。
时间复杂度:O(n*n)

有效的方法:一种有效的方法是观察如果二进制数的最后一位是 1,那么它是奇数,否则它是偶数。所以我们现在的问题被简化为检查最后一个索引值为1的所有子字符串。我们可以通过从末尾遍历来轻松解决这个问题。如果字符串中第 i 个索引的值为 1,则在该索引之前有 i 个奇数子字符串。但这也包括带有前导零的字符串。因此,为了处理这个问题,我们可以使用一个辅助数组来计算第 i 个索引之前 1 的数量。我们只计算一对 1。

下面是这种方法的实现:

C++
// CPP program to count substrings
// with odd decimal value
#include
using namespace std;
 
// function to count number of substrings
// with odd decimal representation
int countSubstr(string s)
{  
    int n = s.length();
     
    // auxiliary array to store count
    // of 1's before ith index
    int auxArr[n] = {0};
     
    if (s[0] == '1')
        auxArr[0] = 1;
     
    // store  count of 1's before
    // i-th  index
    for (int i=1; i=0; i--)   
        if (s[i] == '1')
            count += auxArr[i];   
     
    return count;
}
 
// Driver code
int main()
{
    string s = "1101";   
    cout << countSubstr(s);   
    return 0;
}


Java
// Java program to count substrings
// with odd decimal value
import java.io.*;
import java.util.*;
 
class GFG {
    
// function to count number of substrings
// with odd decimal representation
static int countSubstr(String s)
{
    int n = s.length();
     
    // auxiliary array to store count
    // of 1's before ith index
    int[] auxArr=new int[n];
     
    if (s.charAt(0) == '1')
        auxArr[0] = 1;
     
    // store count of 1's before
    // i-th index
    for (int i=1; i=0; i--)
        if (s.charAt(i) == '1')
            count += auxArr[i];
     
    return count;
}
 
public static void main (String[] args) {
     String s = "1101";
    System.out.println(countSubstr(s));
     
    }
}
 
// This code is contributed by Gitanjali.


Python3
# python program to count substrings
# with odd decimal value
import math
 
# function to count number of substrings
# with odd decimal representation
def countSubstr( s):
 
    n = len(s)
     
    # auxiliary array to store count
    # of 1's before ith index
    auxArr= [0 for i in range(n)]
     
    if (s[0] == '1'):
        auxArr[0] = 1
     
    # store count of 1's before
    # i-th index
    for i in range(0,n):
         
      if (s[i] == '1'):
            auxArr[i] = auxArr[i-1]+1
      else:
            auxArr[i] = auxArr[i-1]
     
     
    # variable to store answer
    count = 0
     
    # traverse the string reversely to
    # calculate number of odd substrings
    # before i-th index
    for i in range(n-1,-1,-1):
        if (s[i] == '1'):
            count += auxArr[i]
     
    return count
# Driver method
s = "1101"
print (countSubstr(s))
 
# This code is contributed by Gitanjali.


C#
// C# program to count substrings
// with odd decimal value
using System;
 
class GFG {
     
// Function to count number of substrings
// with odd decimal representation
static int countSubstr(string s)
{
    int n = s.Length;
     
    // auxiliary array to store count
    // of 1's before ith index
    int[] auxArr = new int[n];
     
    if (s[0] == '1')
        auxArr[0] = 1;
     
    // store count of 1's before
    // i-th index
    for (int i = 1; i < n; i++)
    {
        if (s[i] == '1')
            auxArr[i] = auxArr[i - 1] + 1;
        else
            auxArr[i] = auxArr[i - 1];
    }
     
    // variable to store answer
    int count = 0;
     
    // Traverse the string reversely to
    // calculate number of odd substrings
    // before i-th index
    for (int i = n - 1; i >= 0; i--)
        if (s[i] == '1')
            count += auxArr[i];
     
    return count;
}
 
// Driver Code
public static void Main () {
    string s = "1101";
    Console.WriteLine(countSubstr(s));
    }
}
 
// This code is contributed by vt_m.


PHP
= 0; $i--)
        if ($s[$i] == '1')
            $count += $auxArr[$i];
     
    return $count;
}
 
// Driver code
$s = "1101";
echo countSubstr($s);
 
// This code is contributed by aj_36
?>


Javascript


输出 :

6

时间复杂度:O(n)
辅助空间:O(n)