Count N-length Binary Strings consisting of "11" as substring
Last Updated :
20 Mar, 2023
Given a positive integer N, the task is to find the number of binary strings of length N which contains "11" as a substring.
Examples:
Input: N = 2
Output: 1
Explanation: The only string of length 2 that has "11" as a substring is "11".
Input: N = 12
Output: 3719
Approach: The idea is to derive the number of possibilities of having "11" as a substring for binary representations starting with 0 or 1 based on the following observations:
- If the first bit is 0, then the starting bit does not contribute to the string having "11" as a substring. Therefore, the remaining (N - 1) bits have to form a string having "11" as a substring.
- If the first bit is 1 and the following bit is also 1, then there exists 2(N - 2) strings having "11" as a substring.
- If the first bit is 1 but the following bit is 0, then a string having "11" as a substring can be formed with remaining (N - 2) bits.
- Therefore, the recurrence relation to generate all the binary strings of length N is:
dp[i] = dp[i - 1] + dp[i - 2] + 2(i - 2)
where,
dp[i] is the string of length i having "11" as a substring.
and dp[0] = dp[1] = 0.
Recursive Solution (Naive Solution):
here we have to find the number of possibilities having "11" as a substring for binary representation starting with 0 or 1 based on the observations. so above mentioned approach is for that. but in a recursive solution if we want to find the solution for i then that should be calculated as follows,
binaryStrings(i) = binaryStrings(i-1) + binaryStrings(i-2) + 2(i-2)
which can be solved recursively as follow.
C++
// Recursive C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count binary strings of length N having substring "11"
int binaryStrings(int N) {
// Base Cases
if (N == 0 || N == 1) {
return 0;
}
// Recursively calculate answer for current state
return binaryStrings(N - 1) + binaryStrings(N - 2) + (1 << (N - 2)); // 1<<(i-2) means power of 2^(i-2)
}
// Driver Code
int main()
{
int N = 12;
cout << binaryStrings(N);
return 0;
}
Java
import java.util.*;
public class Main
{
// Function to count binary strings of length N having
// substring "11"
static int BinaryStrings(int N)
{
// Base Cases
if (N == 0 || N == 1) {
return 0;
}
// Recursively calculate answer for current state
return BinaryStrings(N - 1) + BinaryStrings(N - 2)
+ (1
<< (N
- 2)); // 1<<(i-2) means power of 2^(i-2)
}
// Driver Code
public static void main(String[] args)
{
int N = 12;
System.out.println(BinaryStrings(N));
}
}
Python3
# Function to count binary strings of length N having substring "11"
def binaryStrings(N: int) -> int:
# Base Cases
if N == 0 or N == 1:
return 0
# Recursively calculate answer for current state
return binaryStrings(N - 1) + binaryStrings(N - 2) + (1 << (N - 2)) # 1<<(i-2) means power of 2^(i-2)
# Driver Code
if __name__ == "__main__":
N = 12
print(binaryStrings(N))
C#
using System;
public class Program {
// Function to count binary strings of length N having substring "11"
static int BinaryStrings(int N) {
// Base Cases
if (N == 0 || N == 1) {
return 0;
}
// Recursively calculate answer for current state
return BinaryStrings(N - 1) + BinaryStrings(N - 2) + (1 << (N - 2)); // 1<<(i-2) means power of 2^(i-2)
}
// Driver Code
public static void Main() {
int N = 12;
Console.WriteLine(BinaryStrings(N));
}
}
JavaScript
// Function to count binary strings of length N having substring "11"
function binaryStrings(N)
{
// Base Cases
if (N == 0 || N == 1)
{
return 0;
}
// Recursively calculate answer for current state
return binaryStrings(N - 1) + binaryStrings(N - 2) + (1 << (N - 2)); // 1<<(i-2) means power of 2^(i-2)
}
// Driver Code
let N = 12;
console.log(binaryStrings(N));
Optimized Solution:
Follow the steps below to solve the problem:
- Initialize an array, say dp[], of size (N + 1) and assign dp[0] as 0 and dp[1] as 0.
- Precompute the first N powers of 2 and store it in an array, say power[].
- Iterate over the range [2, N] and update dp[i] as (dp[i - 1] + dp[i - 2] + power[i - 2]).
- After completing the above steps, print the value of dp[N] as the result.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count binary strings
// of length N having substring "11"
void binaryStrings(int N)
{
// Initialize dp[] of size N + 1
int dp[N + 1];
// Base Cases
dp[0] = 0;
dp[1] = 0;
// Iterate over the range [2, N]
for (int i = 2; i <= N; i++) {
dp[i] = dp[i - 1]
+ dp[i - 2]
+ (1<<(i-2)); // 1<<(i-2) means power of 2^(i-2)
}
// Print total count of substrings
cout << dp[N];
}
// Driver Code
int main()
{
int N = 12;
binaryStrings(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to count binary strings
// of length N having substring "11"
static void binaryStrings(int N)
{
// Initialize dp[] of size N + 1
int[] dp = new int[N + 1];
// Base Cases
dp[0] = 0;
dp[1] = 0;
// Stores the first N powers of 2
int[] power = new int[N + 1];
power[0] = 1;
// Generate
for(int i = 1; i <= N; i++)
{
power[i] = 2 * power[i - 1];
}
// Iterate over the range [2, N]
for(int i = 2; i <= N; i++)
{
dp[i] = dp[i - 1] + dp[i - 2] + power[i - 2];
}
// Print total count of substrings
System.out.println(dp[N]);
}
// Driver Code
public static void main(String[] args)
{
int N = 12;
binaryStrings(N);
}
}
// This code is contributed by ukasp
Python3
# Python3 program for the above approach
# Function to count binary strings
# of length N having substring "11"
def binaryStrings(N):
# Initialize dp[] of size N + 1
dp = [0]*(N + 1)
# Base Cases
dp[0] = 0
dp[1] = 0
# Stores the first N powers of 2
power = [0]*(N + 1)
power[0] = 1
# Generate
for i in range(1, N + 1):
power[i] = 2 * power[i - 1]
# Iterate over the range [2, N]
for i in range(2, N + 1):
dp[i] = dp[i - 1] + dp[i - 2] + power[i - 2]
# Prtotal count of substrings
print (dp[N])
# Driver Code
if __name__ == '__main__':
N = 12
binaryStrings(N)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to count binary strings
// of length N having substring "11"
static void binaryStrings(int N)
{
// Initialize dp[] of size N + 1
int []dp = new int[N + 1];
// Base Cases
dp[0] = 0;
dp[1] = 0;
// Stores the first N powers of 2
int []power = new int[N + 1];
power[0] = 1;
// Generate
for (int i = 1; i <= N; i++) {
power[i] = 2 * power[i - 1];
}
// Iterate over the range [2, N]
for (int i = 2; i <= N; i++) {
dp[i] = dp[i - 1]
+ dp[i - 2]
+ power[i - 2];
}
// Print total count of substrings
Console.WriteLine(dp[N]);
}
// Driver Code
public static void Main()
{
int N = 12;
binaryStrings(N);
}
}
// This code is contributed by bgangwar59.
JavaScript
<script>
// JavaScript program for the above approach
// Function to count binary strings
// of length N having substring "11"
function binaryStrings(N) {
// Initialize dp of size N + 1
var dp = Array(N + 1).fill(0);
// Base Cases
dp[0] = 0;
dp[1] = 0;
// Stores the first N powers of 2
var power = Array(N+1).fill(0);
power[0] = 1;
// Generate
for (i = 1; i <= N; i++) {
power[i] = 2 * power[i - 1];
}
// Iterate over the range [2, N]
for (i = 2; i <= N; i++) {
dp[i] = dp[i - 1] + dp[i - 2] + power[i - 2];
}
// Print total count of substrings
document.write(dp[N]);
}
// Driver Code
var N = 12;
binaryStrings(N);
// This code contributed by aashish1995
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Count of substrings of a Binary string containing only 1s Given a binary string of length N, we need to find out how many substrings of this string contain only 1s. Examples: Input: S = "0110111"Output: 9Explanation:There are 9 substring with only 1's characters. "1" comes 5 times. "11" comes 3 times. "111" comes 1 time. Input: S = "000"Output: 0 The_Appro
6 min read
Count of substrings of a binary string containing K ones Given a binary string of length N and an integer K, we need to find out how many substrings of this string are exist which contains exactly K ones. Examples: Input : s = â10010â K = 1 Output : 9 The 9 substrings containing one 1 are, â1â, â10â, â100â, â001â, â01â, â1â, â10â, â0010â and â010âRecommen
7 min read
Count ways to generate Binary String not containing "0100" Substring Given the number N, count the number of ways to create a binary string (the string that contains characters as zero or one) of size N such that it does not contain "0100" as a substring. A substring is a contiguous sequence of characters within a string. Examples: Input: N = 4Output: 15Explanation:
15+ min read
Count of binary strings of given length consisting of at least one 1 Given an integer N, the task is to print the number of binary strings of length N which at least one '1'. Examples: Input: 2 Output: 3 Explanation: "01", "10" and "11" are the possible strings Input: 3 Output: 7 Explanation: "001", "011", "010", "100", "101", "110" and "111" are the possible strings
3 min read
Count of substrings in a Binary String that contains more 1s than 0s Given a binary string s, the task is to calculate the number of such substrings where the count of 1's is strictly greater than the count of 0's. Examples Input: S = "110011"Output: 11Explanation: Substrings in which the count of 1's is strictly greater than the count of 0's are { S[0]}, {S[0], S[1]
15+ min read
Count of K length subarrays containing only 1s in given Binary String Given a binary string str, the task is to find the count of K length subarrays containing only 1s. Examples: Input: str = "0101000", K=1Output: 2Explanation: 0101000 -> There are 2 subarrays with 1 ones Input: str = "11111001", K=3Output: 3 Approach: The task can be solved by keeping track of the
4 min read
Counting even decimal value substrings in a binary string Given a binary string of size N. Count all substring that have even decimal value considering binary to decimal conversion from left to right (For example a substring "1011" is treated as 13) Examples : Input : 101Output : 2Explanation : Substring are : 1, 10, 101, 0, 01, 1 In decimal form : 1, 1, 3
9 min read
Count number of substrings of a string consisting of same characters Given a string. The task is to find out the number of substrings consisting of the same characters. Examples: Input: abba Output: 5 The desired substrings are {a}, {b}, {b}, {a}, {bb} Input: bbbcbb Output: 10 Approach: It is known for a string of length n, there are a total of n*(n+1)/2 number of su
6 min read
Count Palindromic Substrings in a Binary String Given a binary string S i.e. which consists only of 0's and 1's. Calculate the number of substrings of S which are palindromes. String S contains at most two 1's. Examples: Input: S = "011"Output: 4Explanation: "0", "1", "1" and "11" are the palindromic substrings. Input: S = "0" Output: 1Explanatio
7 min read
Count of groups of consecutive 1s in a given Binary String Given a binary string S of size N, the task is to find the number of groups of 1s only in the string S. Examples: Input: S = "100110111", N = 9Output: 3Explanation: The following groups are of 1s only: Group over the range [0, 0] which is equal to "1".Group over the range [3, 4] which is equal to "1
5 min read