将“11”替换为“0”后可能的不同二进制字符串的计数
给定一个大小为N的二进制字符串str ,只包含0和1 ,任务是计算所有可能的不同二进制字符串,当子字符串“11”可以被“0”替换时。
例子:
Input: str = “11011”
Output: 4
Explanation: All possible combinations are “11011”, “0011”, “1100”, “000”.
Input: str = “110011100011111”
Output: 48
天真的方法:解决问题的最简单的想法是尝试更改每个可能的子字符串,并找出总共有多少不同的字符串正在形成。
时间复杂度: O(2 N )
辅助空间: O(2 N )
Efficient Approach:这个问题可以通过使用动态规划的概念并借助以下思想有效地解决:
- Consider there are X number of groups of consecutive 1s. Find out in how many ways each of these groups can be modified by changing “11” to “0”. The multiplication of all those values will be the required answer.
- Dynamic programming (say dp[] array) can be used to find the possible combinations of consecutive 1s.
- If the ith element is 0, it cannot be changed. So dp[i] = 1.
- If ith character is 1 it can be kept 1 in dp[i-1] ways or (i-1)th and ith character can be replaced with 0 in dp[i-2] ways.
So dp[i] = dp[i-1]+dp[i-2] in this case
请按照以下步骤解决问题:
- 从i = 0 迭代到 N-1并使用上述观察填充 dp[] 数组。
- 再次从头开始迭代并乘以连续 1 的组合数。
- 返回相乘的值作为答案。
下面是上述方法的实现。
C++
// C++ code to implement above approach
#include
using namespace std;
// Function to return the
// total different combination
// possible
int total_count(string s, int n)
{
int ans = 1, dp[n] = { 0 };
// Base cases
if (s[0] == '1')
dp[0] = 1;
if (s[1] == '1' && s[1] == s[0]) {
dp[1] = 2;
}
else if (s[1] == '1') {
dp[1] = 1;
}
// Iterating through every index
// and storing the total
// combination of string due to
// all the adjacent 1's.
for (int i = 2; i < n; i++) {
if (s[i] == '1') {
if (s[i] == s[i - 1]) {
if (s[i - 1] == s[i - 2]) {
dp[i] = dp[i - 1]
+ dp[i - 2];
}
else {
dp[i] = 2;
}
}
else {
dp[i] = 1;
}
}
}
// Total combinations possible
// by multiplying all the individual
// combinations.
for (int i = 1; i < n; i++) {
if (dp[i - 1] > dp[i]) {
ans = ans * dp[i - 1];
}
}
// Edge case
if (dp[n - 1] != 0) {
ans = ans * dp[n - 1];
}
// Returning the final value
return ans;
}
// Driver code
int main()
{
string str = "11011";
int N;
N = str.size();
// Function call
cout << total_count(str, N);
return 0;
}
Java
// JAVA code to implement above approach
import java.util.*;
class GFG
{
// Function to return the
// total different combination
// possible
public static int total_count(String s, int n)
{
int ans = 1;
int dp[] = new int[n];
for (int i = 0; i < n; ++i) {
dp[i] = 0;
}
// Base cases
if (s.charAt(0) == '1')
dp[0] = 1;
if (s.charAt(1) == '1'
&& s.charAt(1) == s.charAt(0)) {
dp[1] = 2;
}
else if (s.charAt(1) == '1') {
dp[1] = 1;
}
// Iterating through every index
// and storing the total
// combination of string due to
// all the adjacent 1's.
for (int i = 2; i < n; i++) {
if (s.charAt(i) == '1') {
if (s.charAt(i) == s.charAt(i - 1)) {
if (s.charAt(i - 1)
== s.charAt(i - 2)) {
dp[i] = dp[i - 1] + dp[i - 2];
}
else {
dp[i] = 2;
}
}
else {
dp[i] = 1;
}
}
}
// Total combinations possible
// by multiplying all the individual
// combinations.
for (int i = 1; i < n; i++) {
if (dp[i - 1] > dp[i]) {
ans = ans * dp[i - 1];
}
}
// Edge case
if (dp[n - 1] != 0) {
ans = ans * dp[n - 1];
}
// Returning the final value
return ans;
}
// Driver code
public static void main(String[] args)
{
String str = "11011";
int N;
N = str.length();
// Function call
System.out.print(total_count(str, N));
}
}
// This code is contributed by Taranpreet
Python3
# Python3 code to implement the above approach
# function to return the total different
# combination possible
def total_count(s, n):
ans = 1
dp = [0] * n
# base cases
if s[0] == "1":
dp[0] = 1
if s[1] == "1" and s[1] == s[0]:
dp[1] = 2
elif s[1] == "1":
dp[1] = 1
# iterating through every index and storing
# the total combination of strings due to all
# the adjacent 1s
for i in range(2, n):
if s[i] == "1":
if s[i] == s[i - 1]:
if s[i - 1] == s[i - 2]:
dp[i] = dp[i - 1] + dp[i - 2]
else:
dp[i] = 2
else:
dp[i] = 1
# Total combinations possible by multiplying
# all the individual combinations
for i in range(1, n):
if dp[i - 1] > dp[i]:
ans *= dp[i - 1]
# Edge case
if dp[n - 1] != 0:
ans *= dp[n - 1]
# Returning the final value
return ans
# Driver Code
string = "11011"
N = len(string)
# Function Call
print(total_count(string, N))
#This Code was contributed by phasing17
C#
// C# code to implement above approach
using System;
public class GFG{
// Function to return the
// total different combination
// possible
static int total_count(string s, int n)
{
int ans = 1;
int[] dp = new int[n];
for (int i = 0; i < n; ++i) {
dp[i] = 0;
}
// Base cases
if (s[0] == '1')
dp[0] = 1;
if (s[1] == '1'
&& s[1] == s[0]) {
dp[1] = 2;
}
else if (s[1] == '1') {
dp[1] = 1;
}
// Iterating through every index
// and storing the total
// combination of string due to
// all the adjacent 1's.
for (int i = 2; i < n; i++) {
if (s[i] == '1') {
if (s[i] == s[i - 1]) {
if (s[i - 1]
== s[i - 2]) {
dp[i] = dp[i - 1] + dp[i - 2];
}
else {
dp[i] = 2;
}
}
else {
dp[i] = 1;
}
}
}
// Total combinations possible
// by multiplying all the individual
// combinations.
for (int i = 1; i < n; i++) {
if (dp[i - 1] > dp[i]) {
ans = ans * dp[i - 1];
}
}
// Edge case
if (dp[n - 1] != 0) {
ans = ans * dp[n - 1];
}
// Returning the final value
return ans;
}
// Driver code
static public void Main (){
string str = "11011";
int N;
N = str.Length;
// Function call
Console.Write(total_count(str, N));
}
}
// This code is contributed by hrithikgarg03188.
Javascript
输出
4
时间复杂度: O(N)
辅助空间: O(N)