Problem · String
Count Substrings
Learn this problemProblem statement
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
countSubstrings(s: String) → int
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
Examples
Example 1
s = "10101"return = 4The 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.
More Salesforce problems
- Minimize Total Input Cost (for LTMS)Seen Jun 2026
- Count Prime StringsONSITE INTERVIEW · Seen Jun 2026
- Final Pod Counts After LogsOA · Seen May 2026
- ATM Queue Exit OrderPHONE SCREEN · Seen May 2026
- Good Ways to Split an ArrayPHONE SCREEN · Seen May 2026
- Generate Seen Binary StringsOA · Seen May 2026
- Update Pod Counts From LogsOA · Seen May 2026
- Minimum Removals to Balance ArrayOA · Seen May 2026