Count Binary Substrings
Updated
Problem statement
Given a binary string s, count the nonempty substrings that have the same number of 0 and 1 characters, where all the 0 characters sit together and all the 1 characters sit together. A substring is a piece of s made of characters that are next to each other.
Count pieces by where they are, so equal text at different places is counted more than once.
Function
countBinarySubstrings(s: String) → intExamples
Example 1
s = "00110011"return = 6In order of where they start, the valid pieces are 0011, 01, 1100, 10, 0011, and 01. That is six pieces, so the answer is six.
Example 2
s = "10101"return = 4Each pair of neighbors, such as the first two characters, is a valid piece. There are four pairs, so that is four pieces. A longer piece like the first four characters switches between the two digits more than once, so it does not count. The answer is four.
Constraints
1 <= s.length <= 100000.- Every character of
sis0or1.
More DocuSign Onsite Interview questions
- Two Positions That Add Up to a TargetPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Compress Consecutive Integer RangesONSITE INTERVIEW · Seen Aug 2026
- JSON Schema Object ValidatorONSITE INTERVIEW · Seen Aug 2026
- Most Transactions Kept Without Going NegativeONSITE INTERVIEW · Seen May 2025
- Three-Part Splits With a Light MiddleONSITE INTERVIEW · Seen May 2025
- Combine Two Sorted Lists Into OneONSITE INTERVIEW · Seen Apr 2022
- Exact Palindrome CheckPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2022
- Paired Words That Share a SubstringONSITE INTERVIEW · Seen Apr 2022
- Reverse the Characters of a TextPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2022
- Reverse the Order of a Linked ListONSITE INTERVIEW · Seen Feb 2020
Similar questions at other companies
- Longest Spoken PrefixElevenLabs · ONSITE INTERVIEWElevenLabs interview questions
- Palindrome Check Ignoring SymbolsWalmart · ONSITE INTERVIEWWalmart interview questions
- Reverse a String Keeping Dashes in PlaceNykaa · ONSITE INTERVIEWNykaa interview questions
- Reverse Vowels of a StringGoogle · ONSITE INTERVIEWGoogle interview questions
- Valid Palindrome After NormalizationKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Minimum Changes for Palindromic BlocksCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
- Palindrome Match PercentageApple · ONSITE INTERVIEWApple interview questions
- Reverse A StringNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Validate a Word AbbreviationSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Run-Length String CompressionOracle · ONSITE INTERVIEWOracle interview questions