forked from shreyamalogi/DSA-BOOK
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path058_split binary string.cpp
More file actions
50 lines (37 loc) · 878 Bytes
/
Copy path058_split binary string.cpp
File metadata and controls
50 lines (37 loc) · 878 Bytes
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
//author :shreyamalogi
//Split the binary string into substrings with equal number of 0s and 1s
//if count of zero is equal to count of 1 thenm answerwer will be 1
#include <bits/stdc++.h>
using namespace std;
int maxSubStr(string s, int n)
{
int zeroes = 0, ones = 0; // To store the count of 0s and 1s
// To store the count of maximum
// substrings str can be divided into
int cnt = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '0') {
zeroes++;
}
else {
ones++;
}
if (zeroes == ones) {
cnt++;
}
}
if (zeroes != ones) {
return -1;
}
return cnt;
}
int main()
{
string s;
cin>>s;
int n = s.length();
cout << maxSubStr(s, n);
return 0;
}
//0100110101
//4