Count Binary Substrings
Updated
Problem statement
You are given a binary string s. A binary string has only the characters 0 and 1. A substring is a group of characters that sit next to each other in s.
Count the substrings that follow both rules:
- The substring has the same number of 0s and 1s.
- All of its 0s are next to each other, and all of its 1s are next to each other.
Count a substring once for every place it appears, even when the same text shows up more than once. Return the count.
For example, "0011" has two such substrings: "0011" and "01".
Function
getSubstringCount(s: String) → intExamples
Example 1
s = "011001"return = 4The substrings that count are "01" (indexes 0 to 1), "1100" (indexes 1 to 4), "10" (indexes 2 to 3) and "01" (indexes 4 to 5). So the answer is 4.
"0110" has two 0s and two 1s, but it does not count: its two 0s are not next to each other.
Constraints
1 ≤ s.length ≤ 10^5scontains only the characters0and1.
More Dell Technologies interview questions
Similar questions at other companies
- Rearrange String by Alternating EndsHudson River Trading · OAHudson River Trading interview questions
- Alternate String EndsCapital One · OACapital One interview questions
- Run-Length String CompressionSalesforce · OASalesforce interview questions
- Minimum Changes for Palindromic BlocksCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
- Count Binary SubstringsMicrosoft · OAMicrosoft interview questions
- Check a Repeated String as a SubsequenceQuince · OAQuince interview questions
- Reverse String In PlaceThe Walt Disney Company · OA · PHONE SCREENThe Walt Disney Company interview questions
- Lost MessagesSuperhuman · OASuperhuman interview questions
- Reverse a StringFlexTrade · OAFlexTrade interview questions
- Missing WordsVanguard · OAVanguard interview questions