-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcount_binary_substrings.cpp
More file actions
53 lines (53 loc) · 1.2 KB
/
Copy pathcount_binary_substrings.cpp
File metadata and controls
53 lines (53 loc) · 1.2 KB
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
/*
@Author - Jatin Goel
@Institute - IIIT Allahabad
Hardwork definitely pays off.
There is no substitute of hardwork.
There is no shortcut to success.
*/
class Solution {
public:
int countBinarySubstrings(string s) {
int n = s.size();
int zeros[n+5];
int ones[n+5];
memset(zeros,0,sizeof(zeros));
memset(ones,0,sizeof(ones));
int ctr = 0;
for(int i=0;i<s.size();i++)
{
if(s[i] == '0')
ctr++;
else
ctr = 0;
zeros[i] = ctr;
}
ctr=0;
for(int i=0;i<s.size();i++)
{
if(s[i] == '1')
ctr++;
else
ctr = 0;
ones[i] = ctr;
}
int ans =0;
for(int i=0;i<s.size();i++)
{
if(zeros[i] > 0 && i-zeros[i] >= 0)
{
if(ones[i-zeros[i]] >= zeros[i])
ans++;
}
}
for(int i=0;i<s.size();i++)
{
if(ones[i] > 0 && i-ones[i] >= 0)
{
if(zeros[i-ones[i]] >= ones[i])
ans++;
}
}
return ans;
}
};