Problem Brief
Count Substrings
OA
See Salesforce online assessment and hiring insights
Count substrings that satisfy the following two conditions:
- Count of
1sand count of0sis equal. - Should be a block of
0sfollowed by a block of1sor 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
1Example 1
Input
s = "10101"
Output
4
Explanation
The string
"10101" contains the following substrings that satisfy the conditions:
"10": A block of1followed by a block of0."01": A block of0followed by a block of1."10": Another block of1followed by a block of0."01": Another block of0followed by a block of1.