BalancedSubstrings
2015 TCO Parallel 2D · 2015-04-08 · by lg5293
Problem Statement
This problem deals with binary strings: strings in which each character is either '0' or '1'. The characters are interpreted as zeros and ones.
Assume that we have a binary string of length N. Imagine the string as a horizontal lever of length N-1. The weight of the lever is negligible. On the lever, the points with integer coordinates are numbered from 0 (one end of the lever) to N-1 (the other end). Our string represents the distribution of weights on this lever. For each i, if character i of our string is '0', the corresponding point is empty, and if the character is '1', there is a unit weight at that point. Suppose that we place a fulcrum under the point number i. We say that element i of the string is a balance point if the lever is balanced on the fulcrum: the moments of force on either side cancel each other out. A string is called a balanced string if it has at least one balance point. Note that the balance point must be one of the marked points (see examples below).
A formal definition follows. For each valid index i we can compute the torque at i as follows:
- For each element to the left of i, take its value, multiply it by its distance from i, and add all those results together to obtain the value A.
- For each element to the right of i, take its value, multiply it by its distance from i, and add all those results together to obtain the value B.
- The torque at i is computed as (A - B).
For example, the string "10100001" is a balanced string. Its balance point is the (0-based) index i=3. If we put the fulcrum under the lever at this position, we see "101" to the left and "0001" to the right. On the left side we get A = 1*3 + 0*2 + 1*1 = 4, and on the right side we get B = 0*1 + 0*2 + 0*3 + 1*4 = 4, hence A-B is exactly zero.
The string "0001" is also a balanced string, as its last character is a balance point. The string "11" is not a balanced string, as neither of its two characters is a balance point.
You are given a
Substrings that consist of the same characters but occur elsewhere in s are considered different substrings. If they are balanced, each of them should be counted separately. For example, the string "00000" contains four distinct occurrences of the substring "00".
Constraints
- s will have between 1 and 2,500 characters, inclusive.
- Each character in s will be '0' or '1'.
"011" Returns: 4
The balanced substrings in this case are {"0", "1", "1", "01"}
"10111" Returns: 10
The balanced substrings are {"1", "0", "1", "1", "1", "10", "01", "101", "111", "0111"}
"00000" Returns: 15
All substrings in this case are balanced.
"0000001000000" Returns: 91
"100110001001" Returns: 49
Submissions are judged against all 58 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class BalancedSubstrings with a public method int countSubstrings(string s) · 58 test cases · 2 s / 256 MB per case