给定一个正整数N ,任务是找到长度为N的二进制字符串的数量,其中包含“11”作为子字符串。
例子:
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
方法:这个想法是基于以下观察得出将“11”作为以0或1开头的二进制表示的子串的可能性数量:
- 如果第一位为0 ,则起始位对具有“11”作为子串的字符串没有贡献。因此,剩余(N – 1)的比特具有以形成具有“11”作为一个子字符串的字符串。
- 如果第一位是1,位以下也为1,则存在2(N – 2),其具有“11”作为一个子字符串。
- 如果第一位是1,但以下的位为0,则具有“11”作为一个子字符串可以与剩余的(N – 2)形成的位字符串。
- 因此,生成所有长度为N的二进制字符串的递推关系为:
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.
请按照以下步骤解决问题:
- 初始化一个数组,比如dp[] ,大小为(N + 1)并将dp[0]指定为0 ,将dp[1] 指定为0 。
- 预先计算2的前N 个幂并将其存储在一个数组中,比如power[] 。
- 迭代范围[2, N]并将dp[i]更新为(dp[i – 1] + dp[i – 2] + power[i – 2]) 。
- 完成以上步骤后,打印dp[N]的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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
输出
3719
时间复杂度: O(N)
辅助空间: O(N)