Skip to content

Commit ba33e22

Browse files
Create Count Substrings That Satisfy K-Constraint I.cpp
1 parent 3d58e71 commit ba33e22

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int countKConstraintSubstrings(string s, int k) {
4+
int count = 0;
5+
for(int i = 0; i < s.length(); i++){
6+
for(int j = i; j < s.length(); j++){
7+
int countOne = 0;
8+
int countZero = 0;
9+
int length =0;
10+
for(int k = i; k<=j; k++){
11+
if(s[k]=='1') countOne++;
12+
else countZero++;
13+
length++;
14+
}
15+
16+
17+
if(((countZero <= k) || (countOne <=k)) && length > 0) count++;
18+
19+
}
20+
}
21+
22+
return count;
23+
}
24+
};

0 commit comments

Comments
 (0)