Description
Solutions
Count Substrings
📚RELATED PROBLEMS
Count substrings that satisfy the following two conditions:
- Count of
1s
and count of0s
is equal. - Should be a block of
0s
followed by a block of1s
or vice versa.
Examples of such substrings are "0011"
, "01"
, "10"
, "1100"
.
You can count repetitions, for example, "10101"
has 4 such substrings: "10"
, "01"
, "10"
, "01"
.
Function Description
Complete the function countSubstrings
in the editor.
countSubstrings
has the following parameter:
String s
: the binary string to be checked
Returns
int: the number of substrings that satisfy the conditions
Example 1:
Input: s = "10101"
Output: 4
Explanation:The string"10101"
contains the following substrings that satisfy the conditions:There are a total of 4 such substrings, so the answer is 4.
"10"
: A block of1
followed by a block of0
."01"
: A block of0
followed by a block of1
."10"
: Another block of1
followed by a block of0
."01"
: Another block of0
followed by a block of1
.
Constraints:
N/A

Related Problems
Testcase
Result
Case 1
input:
output: