-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path07_optimal_binary_strings_recursion.cpp
125 lines (99 loc) · 3.69 KB
/
07_optimal_binary_strings_recursion.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
Topic - Optimal Binary Strings Recursion
# Count Binary Strings
Given a positive integer N, count all possible distinct binary strings of length N such that
there are no consecutive 1’s.
Eg: Input : N = 3
Output : Count = 5
// The 5 strings are 000, 001, 010, 100, 101
Input : N = 2
Output : Count = 3
// The 3 strings are 00, 01, 10
Explanation: When length is 2.
Then there are 4 options i.e (00, 01, 10, 11) when placing binary on 2 positions.
Now, 11 is discarded as it has consecutive ones.
So, count is 3.
*/
#include <iostream>
using namespace std;
// function to Count Binary String which have No consecutive ones
int countBinaryStrings(int n, int last_digit)
{
// base case
if(n == 0)
{
return 0;
}
// if only one digit is left
if(n == 1)
{
if(last_digit){ // if the last digit is 1
return 1;
}
else{ // otherwise, if the last digit is 0
return 2;
}
}
// rec case:
// if the last digit is 0, we can have both 0 and 1 at the current position
if(last_digit == 0)
{
return countBinaryStrings(n-1, 0) + countBinaryStrings(n-1, 1) ;
}
// if the last digit is 1, we can only have 0 at the current position
else
{
return countBinaryStrings(n-1, 0);
}
}
// function to drive code
int main()
{
int total_digits;
cout << "Enter length: ";
cin >> total_digits;
cout << "Count: ";
cout << countBinaryStrings(total_digits, 0);
cout << endl;
return 0;
}
/*
OUTPUT:
Case 1:
Enter length: 2
Count: 3
Case 2:
Enter length: 3
Count: 5
APPROACH:
Let f(n) give the count of binary sequences of length n without adjacent 1's.
f(0) = 1 //There is exactly one way to list 0 items.
f(1) = 2 //It's either a 0 or a 1.
Now consider the addition of an nth digit.
If the nth digit is 0, it may be follow any legal sequence of length (n-1)
If the nth digit is 1, then the (n-1)th digit must be a 0.
These sequences can all be obtained by taking every sequence of length (n-2) and appending a 0.
Therefore: f(n) = f(n-1) + f(n-2)
<--(n-1)--> (last-digit) (n-1) (last-digit)
___________ | ___________ |
|_|_|_|_|_|_|0| |_|_|_|_|_|_|1|
/ \ |
/ \ |
_________/_ __\________ _____|_____
|_|_|_|_|_|0|0| |_|_|_|_|_|1|0| |_|_|_|_|_|0|1|
When last digit is 0, then When last digit is 1, then
f(n) = f(n-1) + f(n-1) f(n) = f(n-1)
Eg: when n = 5
(5,0)
/ \
/ \
(4,0) (4,1)
/ \ |
(3,0) (3,1) (3,0)
/ \ | / \
(2,0) (2,1) (2,0) (2,0) (2,1)
/ \ | / \ / \ |
(1,0) (1,1) (1,0) (1,0) (1,1) (1,0) (1,1) (1,0)
Reference: https://www.techiedelight.com/find-n-digit-binary-strings-without-consecutive-1s/
https://leetcode.com/discuss/general-discussion/1287402/count-number-of-binary-strings-without-consecutive-1s
*/