Skip to content

Commit e717f96

Browse files
authored
Create CountNumBinaryStrings
1 parent 07aee8b commit e717f96

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
public class CountNumBinaryStr {
2+
public static long startTime;
3+
public static long endTime;
4+
public static void startAlgo() {
5+
startTime=System.currentTimeMillis();
6+
}
7+
public static long endAlgo() {
8+
endTime=System.currentTimeMillis();
9+
return endTime-startTime;
10+
}
11+
public static int numStrIS(int n) {
12+
int[] zeros=new int[n];
13+
int []ones=new int[n];
14+
//seed
15+
zeros[0]=1;
16+
ones[0]=1;
17+
for(int i=1;i<n;i++) {
18+
zeros[i]=zeros[i-1]+ones[i-1];
19+
ones[i]=zeros[i-1];
20+
}
21+
int ans=zeros[n-1]+ones[n-1];
22+
return ans;
23+
}
24+
private class Binary{
25+
int ones;
26+
int zeros;
27+
int ans;
28+
Binary(int ones,int zeros){
29+
this.ones=ones;
30+
this.zeros=zeros;
31+
this.ans=0;
32+
}
33+
Binary(){}
34+
}
35+
public Binary numStrR(int n) {
36+
if(n==1) {
37+
Binary br=new Binary(1,1);
38+
return br;
39+
}
40+
Binary mr=new Binary();
41+
Binary rr=numStrR(n-1);
42+
mr.zeros=rr.zeros+rr.ones;
43+
mr.ones=rr.zeros;
44+
mr.ans=mr.zeros+mr.ones;
45+
return mr;
46+
}
47+
public static int countStrings(int n, int lastDigit)
48+
{
49+
if (n == 0) {
50+
return 0;
51+
}
52+
53+
// if only one digit is left
54+
if (n == 1) {
55+
return (lastDigit == 1) ? 1: 2;
56+
}
57+
58+
// if last digit is 0, we can have both 0 and 1 at current pos
59+
if (lastDigit == 0) {
60+
return countStrings(n - 1, 0) + countStrings(n - 1, 1);
61+
}
62+
// if last digit is 1, we can have only 0 at current position
63+
else {
64+
return countStrings(n - 1, 0);
65+
}
66+
}
67+
68+
69+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy