Count ways to form N Sized Strings with at most two adjacent different pair


In this problem, we will count the number of binary strings of size N, containing at most 2 adjacent distinct pair of characters. It means the string should contain, at most, 2 '01' 0r '10' pairs.

The first approach is that generate all binary strings, and if any binary string contains less than or equal to 2 distinct pairs of characters, include its count in the result.

For the optimal solution, we can count a number of binary strings containing 0, 1, and 2 adjacent different pairs and sum them.

Problem statement - We have given a positive integer N, representing the size of the binary string. We need to count the number of binary strings of size N containing at most 2 adjacent pairs of characters with different character values.

Sample examples

Input

N = 3

Output

8

Explanation - All strings of length 3 contain at most 2 adjacent different pairs.

The strings are 000, 001, 010, 100, 011, 110, 101, 111

Input

N = 4

Output

14

Explanation - The valid binary strings are 0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111. We can observe that all string contains at most 2 adjacent different pairs.

Input

N = 5

Output

22

Explanation - We can generate total 2^5 = 32 binary strings of length 5 but only 22 are valid according to the condition given in the problem.

The valid binary strings are 00000, 00001, 00010, 00011, 00100, 00101, 00110, 00111, 01000, 01001, 01010, 01011, 01100, 01101, 01110, 01111, 10000, 10001, 10010, 10011, 10100, 10101, 10110, 10111, 11000, 11001, 11010, 11011, 11100, 11101, 11110, 11111.

Approach 1

In this approach, we will use the recursive function to generate all binary strings. For each binary string, we check how many adjacent different pair it contains. If it is less than 3, we increment the resultant count value.

Algorithm

Step 1 - Initialize the 'current' string with N 0's.

Step 2 - Also, initialize the 'cnt' with 0 to store the count of valid binary strings and pass it as a reference parameter in the getBinaryStr() function.

Step 3 - In the getBinaryStr() function, if the index is equal to the string length, we get the binary string and follow the below steps to count the number of different adjacent pairs.

Step 3.1 - Initialize the 'diff' variable with 0 to store the number of adjacent different pairs.

Step 3.2 - Start traversing the string, and if the character at ith and (i + 1)th index is not the same, increment the 'diff' value by 1.

Step 3.3 - After traversing the string, if the 'diff' value is less than or equal to 2, increment the 'cnt' value by 1, and execute the 'return' statement.

Step 3.4 - Insert '0' at the current index, and make a recursive function call by incrementing the index value by 1.

Step 3.5 - Insert '1' at the current index, and make a recursive function call by incrementing the index value by 1.

Step 4 - In the countStrings() function, print the 'cnt' value.

Example

#include <bits/stdc++.h>
using namespace std;

void getBinaryStrs(int len, string ¤t, int index, int &cnt) {
    // Base case
    if (index == len) {
        // Checking the number of different adjacent characters
        int diff = 0;
        for (int i = 0; i < len - 1; i++) {
            if (current[i] != current[i + 1]) {
                diff++;
            }
        }
        if (diff <= 2) {
            cnt++;
        }
        return;
    }
    // Put '0' at the current index and make a recursive call
    current[index] = '0';
    getBinaryStrs(len, current, index + 1, cnt);
    // Put '1' at the current index and make a recursive call
    current[index] = '1';
    getBinaryStrs(len, current, index + 1, cnt);
}
void countStrings(int len) {
    string current(len, '0'); // String initialized with all '0's
    int cnt = 0;
    getBinaryStrs(len, current, 0, cnt);
    cout << "The number of ways to create a string of size N according to the given conditions is " << cnt;
}
int main() {
    int N = 3;
    countStrings(N);
    return 0;
}

Output

The number of ways to create a string of size N according to the given conditions is 8

Time complexity - O(N*2N) to generate all binary strings and traverse each string.

Space complexity - O(N) to store the binary string of length N.

Approach 2

In this approach, we will count the number of binary strings of length N containing the atmost 0, 1, and 2 adjacent different pairs. We will use some mathematical formulas to get the output.

0 adjacent pair containing distinct characters

We can construct only two binary strings containing no pairs with different adjacent values. The first contains all zeros, and the second contains all 1's.

1 adjacent pair containing distinct characters

For 1 adjacent pair with distinct characters, we can have only one 01 or 10 pair in the resultant string. So, we can have 2*(N - 1) options for a string of length N.

Here, 2 is for the 10 and 01 pair, and (N - 1) is for different numbers of 1's and 0's in the string. For N = 4, we can construct 0111, 0011, 0001 total of 3 (4 - 1) strings containing the '01' pair. Also, we can construct another 3 strings containing the '10' pair only once.

2 adjacent pair containing distinct characters

For a string containing 2 adjacent pairs with distinct characters, we can have 010 or 101 patterns.

For the '101' pattern, we always require one 1 at the start and one at the end. So, we can have (N - 2) ways to put middle zeros in the string. For example, starting from 2 to N - 1, 2 to N - 2, 2 to N - 3, and so on.

In this way, we can start putting 0's from the 3rd index and have (N - 3) ways to put zeros in the middle.

So, the total ways to put zeros in the 101 pattern is (N - 2) + (N - 3) + (N - 4) + … + 2 + 1, which is equal to (N - 2) * (N - 1) /2. Here, we used the (a) * (a + 1)/2 formula of summation of 1 + 2 + 3 + … + (a - 1) + a series.

We also need to count strings containing 010 patterns. So, the total number of required binary strings is 2*(N - 2)*(N - 1)/2, which is equal to (N - 1)*(N - 2).

Our final formula to get all valid substrings is given below.

2 + 2 * (len - 1) + (len - 2) * (len - 1)

Example

In this example, we used the above formula to count the binary string, which has at most 2 adjacent pairs with distinct values.

#include <bits/stdc++.h>
using namespace std;

long long countStrings(long long len){
   // Sum results of all cases
   return 2 + 2 * (len - 1) + (len - 2) * (len - 1);
}
int main(){
   int N = 3;
   cout << "The number of ways to create a string of size N according to the given conditions is " << countStrings(N);
   return 0;
}

Output

The number of ways to create a string of size N according to the given conditions is 8

Time complexity - O(1), as we use mathematical formulas to get the answer.

Space complexity - O(1), as we don't use any extra space.

The second approach is one of the best to find the count of binary strings by performing the multiplication and summation of the string length. We observed the pattern according to the conditions in the problem statement and prepared a formula.

Updated on: 29-Aug-2023

74 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements