Number of Substrings With Only 1s
10 Prerequisite LeetCode Problems
For “1513. Number of Substrings With Only 1s”, the following are a good preparation:
“3. Longest Substring Without Repeating Characters”: This problem helps in understanding the concept of substrings in a string.
“5. Longest Palindromic Substring”: This problem helps in understanding how to handle substrings in a string.
“28. Implement strStr()”: This problem helps in understanding how to locate substrings within a larger string, a key concept needed for this problem.
“72. Edit Distance”: This problem helps in understanding string manipulation which is important for the given problem.
“76. Minimum Window Substring”: This problem gives a good practice of handling substrings in a larger string.
“139. Word Break”: This problem helps in dealing with substrings in a given string.
“159. Longest Substring with At Most Two Distinct Characters”: This problem helps in understanding the constraints in a substring.
“300. Longest Increasing Subsequence”: Though this problem is related to sequences, the concept of finding sequences with certain properties can be helpful.
“395. Longest Substring with At Least K Repeating Characters”: This problem helps in understanding substrings with specific repeating characters.
“647. Palindromic Substrings”: This problem helps in understanding the count of substrings with a specific property, similar to the problem at hand.
Problem Classification
This problem belongs to the domain of “String Processing” and “Combinatorics”.
‘What’ Components:
 A binary string ’s’ as input.
 Identify substrings which consist only of ‘1’s.
 Count the total number of such substrings.
 Return the count modulo 10^9 + 7.
This is a “Counting” problem, where we’re interested in the number of occurrences of a certain property (in this case, substrings of ‘1’s). The problem involves examining each substring and determining if it meets the specific condition.
The method to solve this might involve iterating over the string, counting sequences of ‘1’s, and using a formula to calculate the total number of substrings that could be formed from a sequence of a certain length. The final answer should be calculated under modulo operation as per the problem’s requirement.
Clarification Questions
Can the binary string ’s’ contain characters other than ‘0’ and ‘1’? The problem statement mentions that s[i] is either ‘0’ or ‘1’, but confirming this explicitly could be beneficial.
Should an empty string or a string consisting of only ‘0’s return 0, as they do not contain any ‘1’s?
The problem statement mentions that the output should be returned modulo 10^9 + 7. Should this operation be applied after summing up the count of all substrings, or should it be applied at each step of the calculation?
If the input string ’s’ has a length exceeding the specified constraint (105), how should the problem be handled?
Does the problem guarantee that the input will always be a valid binary string (a string of only ‘0’s and ‘1’s), or do we need to handle potential validation of the input?
Are we allowed to use builtin libraries/functions for the calculation of combinations or permutations, if required, for the solution?
Problem Analysis and Key Insights
The key insights from analyzing the problem statement are as follows:
The problem is about processing strings and counting certain types of substrings. Specifically, the task is to count substrings that consist only of ‘1’s in a binary string.
A single ‘1’ in the string also counts as a substring with all characters as ‘1’s.
When there are ’n’ continuous ‘1’s in the string, there are n*(n+1)/2 total substrings consisting of ‘1’s, according to the formula for the sum of the first ’n’ natural numbers. This observation could guide the implementation of the solution.
The final answer can potentially be very large, as the length of the string can be up to 10^5. Therefore, the problem asks for the result to be returned modulo 10^9 + 7.
The problem has welldefined constraints. The length of the binary string is between 1 and 10^5, and the string contains only ‘0’s and ‘1’s.
From these insights, we can categorize this problem as a string processing problem with aspects of combinatorics and modulus arithmetic.
Problem Boundary
The scope of this problem involves understanding and applying concepts from string processing, combinatorics, and modular arithmetic.
Specifically, the tasks within the scope include:
Processing a binary string: You need to navigate through the given string character by character and identify the substrings consisting of ‘1’s.
Applying combinatorics: Count the number of substrings that consist only of ‘1’s in the binary string. If there are ’n’ continuous ‘1’s in the string, there are n*(n+1)/2 total substrings, according to the formula for the sum of the first ’n’ natural numbers.
Applying modular arithmetic: Given that the answer can potentially be large, you need to take the modulo 10^9 + 7 of the answer.
The problem does not extend to other string operations, different types of strings, or different counting methods. Additionally, error checking (like validating input) is not within the scope of this problem since the constraints specify that the input will always be a binary string of a certain length.
Establishing the boundary of a problem involves clearly defining the limits and constraints that are provided in the problem statement. For this problem, the boundaries are:
The length of the input string: The string length is at least 1 and at most 10^5.
The content of the string: The string consists only of ‘0’s and ‘1’s. This is a binary string.
The expected output: The function should return an integer that represents the count of all substrings with only ‘1’s. The count should be returned modulo 10^9 + 7 because the answer can be large.
The requirement of counting substrings: Only substrings that consist entirely of ‘1’s need to be counted. Substrings with ‘0’s are not relevant to the problem.
Problemsolving approach: A direct approach is needed to navigate the string and find the count of substrings. Other indirect methods or usage of advanced data structures or algorithms are not expected.
These boundaries help to focus the problemsolving approach and prevent the solution from becoming overly complex or straying into unnecessary areas. They define the scope of potential solutions and ensure they stay within the problem’s requirements.
Distilling the Problem to Its Core Elements
Fundamental Concept: This problem is based upon the principle of substrings in a string and the count of certain types of substrings. It involves the concept of string manipulation and mathematical counting.
Simple Description: Imagine a line of people, some of them are wearing a red hat (‘1’) and some are not wearing a hat (‘0’). You have to count all the groups of people who are standing next to each other and are wearing red hats. A group can be a single person or several people.
Core Problem: We are trying to determine the total number of ‘1’sonly substrings in a given binary string. In simpler terms, we need to count all sequences of consecutive ‘1’s in the string.
Key Components: The key components of this problem are:
 Input: a binary string.
 Substrings: sequences of ‘1’s in the binary string.
 Count: total number of such substrings.
Minimal Set of Operations:
 Scan the string from left to right.
 Keep track of consecutive ‘1’s.
 When a ‘1’s sequence ends, calculate the number of substrings that can be formed from that sequence. Add it to the total count.
 Repeat this process until the end of the string.
 Return the total count modulo 10^9 + 7.
Visual Model of the Problem
You can visualize this problem with a simple diagram. Consider the string ‘0110111’. You can represent it like this:
0 1 1 0 1 1 1
As you can see, there are several sequences of ‘1’s in the string. Each of these sequences can form a certain number of substrings. For instance, a sequence of two ‘1’s can form two substrings (‘1’, ‘11’), while a sequence of three ‘1’s can form six substrings (‘1’, ‘1’, ‘1’, ‘11’, ‘11’, ‘111’).
This can be illustrated as follows:
0 1 1 0 1 1 1
^ ^ ^ ^ ^
The arrows point to the ‘1’s in the string. Each sequence of ‘1’s (marked by consecutive arrows) can form substrings. The problem asks you to count all these substrings.
This visualization helps to understand the problem better. It shows that you need to traverse the string, keep track of sequences of ‘1’s, and count the substrings formed by each sequence.
Problem Restatement
The task is to analyze a binary string, which contains only zeros and ones. Our goal is to identify and count all the continuous substrings that contain only ‘1’s. These substrings could be of varying lengths  from a single ‘1’ to a series of ‘1’s.
The output needs to be the total count of such substrings across the entire binary string. The important point to note here is that the same substring appearing at different places in the string is counted as separate substrings. For example, if we have two ‘1’s in separate places, we count them as two individual substrings.
Also, it’s mentioned that the binary string can be quite long, up to 10^5 characters. Therefore, an efficient solution is necessary to handle such large inputs. Additionally, because the number of substrings can get very large, we need to return the result modulo 10^9 + 7 to prevent integer overflow.
To sum up, we need to parse the given binary string, identify all continuous substrings of ‘1’s, and count them, considering each appearance of the same substring as unique. We then return this count, ensuring we don’t exceed the specified limit by using modulo operation.
Abstract Representation of the Problem
We are given a sequence of binary digits (0s and 1s). Our task is to identify and count all continuous sequences of 1s in the given binary sequence. The sequences can be of variable lengths, ranging from a single 1 to a series of 1s. Each occurrence of the same sequence is considered a unique entity.
The size of the input sequence can be quite large, so an efficient solution is needed to handle large input sizes. Also, since the number of sequences can get significantly large, we return the result modulo a large number (10^9 + 7) to prevent integer overflow.
Thus, in abstract terms, this problem involves sequence processing and counting, with attention to handling large numbers and ensuring computational efficiency.
Terminology
There are a few specialized terms and concepts that are important for understanding this problem:
Binary String: A binary string is a sequence of 1s and 0s. In computer science, it’s a fundamental concept and is often used to represent data at its most basic level. In this problem, our input is given as a binary string.
Substring: A substring is a contiguous sequence of characters within a string. In this problem, we’re asked to count substrings that consist only of 1s.
Modulo Operation: The modulo operation finds the remainder of division of one number by another. In many competitive programming problems, due to the possibility of the answer being a very large number, we are asked to return the answer modulo 10^9 + 7. This is a way to prevent overflow and keep the answer within a reasonable range.
Continuous Sequence: A sequence of elements is said to be continuous if it is unbroken. In the context of this problem, a continuous sequence of 1s means that there are no 0s between any two 1s in the sequence.
Time Complexity: Time complexity of an algorithm quantifies the amount of time taken by an algorithm to run, as a function of the size of the input to the program. For this problem, we need an efficient solution that can handle large input sizes, which makes time complexity an important factor.
Problem Simplification and Explanation
Let’s break down the problem. You’re given a binary string, which is just a series of 1s and 0s. What you need to do is find all the substrings that are composed entirely of 1s.
To understand the concept of a substring, imagine you have a necklace with beads of two different colors, say black and white. A substring would be equivalent to selecting a continuous chain of beads without breaking the necklace.
For this problem, you’re looking for chains that contain only white beads (the 1s in our binary string). Each individual white bead is a valid chain, but so is a series of two, three, or more white beads that are next to each other. However, as soon as you encounter a black bead (or a 0 in the binary string), you have to stop and start a new chain.
The task is to count all these possible chains of white beads and return that number. The only twist is, the total number can get quite large. So instead of returning the actual total, you return the remainder when that total is divided by 1,000,000,007 (this is what “modulo 10^9 + 7” means).
So, essentially, this problem is about counting specific sequences (chains of white beads) in a larger sequence (the whole necklace), with a mathematical twist at the end to keep the numbers manageable.
Constraints
The problem does lend itself to a couple of insights that could be exploited:
Binary String: The string consists only of ‘0’s and ‘1’s. This simplification can be exploited because we only need to count the substrings that consist of ‘1’s, so the ‘0’s serve as clear delimiters or boundaries between the substrings of ‘1’s. Essentially, the ‘0’s partition the string into chunks, each of which can be considered independently.
Continuity of ‘1’s: When we encounter a sequence of ‘1’s, each additional ‘1’ in a sequence creates more substrings than the last. This is because each ‘1’ can create a substring of its own, but it also pairs with each of the previous ‘1’s to create new, longer substrings. This insight leads to a solution where we can simply count the number of continuous ‘1’s and add it to a running total.
Modulo Operation: The use of the modulo operation is a common technique to prevent overflow when dealing with potentially very large numbers, which is common in counting problems like this one. The specific modulus (10^9 + 7) is a large prime number, which is often used in these cases due to its mathematical properties.
String Length Constraint: The string length of up to 10^5 is an important constraint that indicates that an efficient solution is necessary, but it also suggests that solutions with time complexity of O(n) are feasible.
Recognizing these characteristics can lead us to design a more efficient solution, allowing us to tackle the problem in a systematic and strategic manner.
The constraints of the problem can guide us to some key insights:
Binary Input: The input string consists only of ‘0’s and ‘1’s. This implies that we need not worry about case sensitivity, special characters, or the full range of ASCII characters, simplifying our implementation.
Size of the Input: The input string can be quite large (up to 10^5 characters). This suggests that an algorithm with linear time complexity would be ideal. An algorithm with higher time complexity (like quadratic or cubic) would likely be too slow for larger inputs. Therefore, we should aim for a solution that can handle each character in the string once.
Output Modulo 10^9 + 7: The requirement to return the answer modulo 10^9 + 7 is common in problems where the result can be very large. This prevents overflow issues and keeps the result within the range of typical integer data types.
Substrings of ‘1’s: We’re interested in contiguous substrings of ‘1’s. This gives us a natural strategy to tackle the problem  iterate through the string, and whenever we hit a ‘1’, consider it as a start of a new substring and try to find its end, counting the potential substrings as we go.
These insights from the constraints help us to start thinking about the possible algorithms for solving the problem. The goal is to find an approach that respects these constraints and takes advantage of the opportunities they offer for simplification.
Case Analysis
Here are some categorized examples covering different aspects of the input space:
Example 1  Minimum length input (Edge Case)
Input: s = “0”
Output: 0
Explanation: The string is at the minimum length allowed by the constraints (1 character). The single character is ‘0’, so there are no substrings with all ‘1’s. Therefore, the output is 0.
Example 2  All ‘1’s
Input: s = “1111”
Output: 10
Explanation: In this case, the string consists of all ‘1’s. Every substring of ‘1’s is a valid result. There are 1 fourlength substring, 2 threelength substrings, 3 twolength substrings, and 4 onelength substrings. So, the total is 1 + 2 + 3 + 4 = 10.
Example 3  Alternating ‘0’s and ‘1’s
Input: s = “010101”
Output: 3
Explanation: Here, ‘1’s are not contiguous, so we can only count the individual ‘1’s as valid substrings. There are three ‘1’s, hence the output is 3.
Example 4  All ‘0’s (Edge Case)
Input: s = “0000”
Output: 0
Explanation: In this case, the string consists of all ‘0’s. As there are no ‘1’s, there are no substrings that fulfill the criteria, so the output is 0.
Example 5  ‘1’s Enclosed by ‘0’s
Input: s = “01110”
Output: 6
Explanation: Here, the ‘1’s are grouped together and surrounded by ‘0’s. Inside this group, we can form 1 threelength substring, 2 twolength substrings, and 3 onelength substrings. So, the total is 1 + 2 + 3 = 6.
These examples cover a variety of scenarios, including edge cases with minimum length, all ‘1’s, all ‘0’s, and alternating ‘0’s and ‘1’s. This analysis should help in understanding the problem and formulating a robust solution.
Visualization for problems dealing with string operations, such as this one, often involves illustrating the problem as a sequence of characters or using diagrams to represent the concepts.
Here are visual representations of the examples provided:
Example 1  Minimum length input (Edge Case)
s = “0”
No valid substring.
Example 2  All ‘1’s
s = “1111”
Substrings: “1”, “1”, “1”, “1”, “11”, “11”, “11”, “111”, “111”, “1111”
Example 3  Alternating ‘0’s and ‘1’s
s = “010101”
Substrings: “1”, “1”, “1”
Example 4  All ‘0’s (Edge Case)
s = “0000”
No valid substring.
Example 5  ‘1’s Enclosed by ‘0’s
s = “01110”
Substrings: “1”, “1”, “1”, “11”, “11”, “111”
In each case, the substrings are the sequences of ‘1’s of varying lengths. In scenarios with alternating ‘0’s and ‘1’s or all ‘0’s, there are fewer or no valid substrings respectively, since ‘1’s need to be contiguous to form valid substrings. In scenarios where ‘1’s are grouped together or the entire string is made up of ‘1’s, there are more valid substrings due to the contiguity and count of ‘1’s.
From the analysis of different cases, there are several key insights:
The number of substrings depends on the number of continuous ‘1’s: A key insight from the examples is that the count of valid substrings is dependent on the number of continuous ‘1’s. Whenever a ‘1’ is encountered, it can form substrings with the following ‘1’s, if any, until a ‘0’ is encountered. This can be seen in the examples where a series of ‘1’s generate multiple valid substrings.
More the number of continuous ‘1’s, more the valid substrings: As seen from the examples, if the number of continuous ‘1’s increases, the number of substrings with all ‘1’s also increases. This is due to the fact that more combinations of ‘1’s are possible with a higher count.
Single isolated ‘1’s only contribute one valid substring: When there’s only a single ‘1’ surrounded by ‘0’s, it forms just one valid substring itself. For it to contribute more, it must be in a sequence with other ‘1’s.
String with no ‘1’s: In the edge case where there are no ‘1’s in the string, there are no valid substrings.
These insights can guide the design of an algorithm to solve this problem. It points towards a solution where we would count the number of consecutive ‘1’s in the string, and for each group of ‘1’s, calculate the number of possible substrings.
Identification of Applicable Theoretical Concepts
There are mathematical and algorithmic concepts that can help simplify the problem.
Arithmetic Progression: The concept of Arithmetic Progression (AP) can be utilized here. In AP, each term after the first is obtained by adding a constant difference to the preceding term. For instance, 2, 4, 6, 8, 10 is an AP because difference between any two consecutive terms in the series (constant) is the same (2). If we consider a continuous string of ‘1’s of length ’n’, the number of substrings within this continuous string can be calculated as the sum of first ’n’ natural numbers, which follows AP. The sum can be calculated by the formula
n*(n+1)/2
.Sliding Window or Two Pointers: The task requires us to find substrings within a string, and a common strategy for such problems is using a sliding window or two pointer approach. We could maintain a window which holds the current sequence of ‘1’s, and for each new ‘1’, calculate the number of new substrings added.
These concepts can be combined to solve the problem effectively. We can traverse the string with a sliding window approach, and for each group of ‘1’s, we calculate the number of substrings using the Arithmetic Progression formula.
Simple Explanation
Let’s use an analogy with a popular game, “Hopscotch”.
Imagine you’re playing a game of Hopscotch. The Hopscotch track is made up of squares, and some squares are marked with a special sign (let’s say, a star), and others are not. In our scenario, the entire Hopscotch track is the binary string, and the squares with a star are the ‘1’s in the string.
Now, let’s add some rules to this game. You can only hop on squares with a star. A single hop is like a substring of one ‘1’. If you hop on two stars in a row without stopping, it’s like having a substring of ‘11’. Three stars in a row hopped without stopping will be ‘111’, and so on.
The goal of the game is to count all possible hop sequences. So, if you have four stars in a row, you can hop one by one (four possible ways), or two at a time (three possible ways), or three at a time (two ways), or all four at once (one way). So, in total, there are ten possible hop sequences for four stars in a row.
In this problem, your task is to count all possible hop sequences (or, in terms of the original problem, all substrings of ‘1’s) on the entire Hopscotch track (the entire string). You need to count all sequences for all groups of consecutive stars.
Remember, though, you cannot combine stars from different groups. That is, if you have two stars, then a square without a star, then two more stars, you can’t count a hop sequence of four stars  you have to count the two groups separately. That’s just like in the problem, where we’re only looking at substrings of consecutive ‘1’s.
Problem Breakdown and Solution Methodology
To solve this problem, we need to find all substrings that consist of the character ‘1’ in the given binary string. Let’s break down the problemsolving process:
Traverse the binary string: Start from the beginning of the string and inspect each character one by one.
Track consecutive ‘1’s: Whenever we encounter a ‘1’, we increment a counter. This counter keeps track of the current sequence of consecutive ‘1’s. When we encounter a ‘0’, we reset this counter back to zero.
Count substrings of ‘1’s: Each time we encounter a ‘1’, we add the value of the counter to our total count of substrings. This is because the number of substrings that end with the current ‘1’ is equal to the number of ‘1’s we have encountered consecutively so far. For example, if we have encountered three ‘1’s in a row, we can form three substrings that end with the current ‘1’: ‘1’, ‘11’, ‘111’.
Modulo operation: Since the problem asks for the count of substrings modulo 10^9+7, we perform this operation each time we add to the count to ensure the number does not exceed this limit.
Continue until the end of the string: Repeat steps 2 to 4 until we have inspected all characters in the string. The final count of substrings is our answer.
Let’s consider the string ‘0110111’ as an example to illustrate the approach:
 We start with a count of 0.
 The first character is ‘0’, so we don’t do anything.
 The second character is ‘1’. We increment our counter (to 1) and add this to our count (now 1).
 The third character is ‘1’. We increment our counter (to 2) and add this to our count (now 3).
 The fourth character is ‘0’. We reset our counter to 0.
 The fifth character is ‘1’. We increment our counter (to 1) and add this to our count (now 4).
 The sixth character is ‘1’. We increment our counter (to 2) and add this to our count (now 6).
 The seventh character is ‘1’. We increment our counter (to 3) and add this to our count (now 9).
 Having reached the end of the string, we stop and return the final count of substrings, which is 9.
In terms of how specific operations or changes in the problem’s parameters would affect the solution, increasing the length of the string would generally increase the number of substrings, especially if the string contains many ‘1’s. However, the count is always modulo 10^9+7, so the final result will still be within a manageable range.
Inference of ProblemSolving Approach from the Problem Statement
There are several key terms and concepts in this problem that guide the solution approach:
Binary String: This problem involves a string composed only of ‘0’s and ‘1’s. This simplifies the problem as we only have to focus on two types of characters.
Substring: This refers to a contiguous sequence of characters within a string. In this problem, we’re specifically interested in substrings composed only of ‘1’s.
All characters 1’s: This specification tells us that we’re looking for substrings that consist only of the character ‘1’. It informs our approach by leading us to count sequences of consecutive ‘1’s in the string.
Modulo 10^9 + 7: This is a common operation in programming contests and coding problems to prevent overflow of integer variables and to keep results within a manageable range. In this problem, it informs us that we need to perform this operation each time we update our count of substrings.
Counter Variable: A common strategy in many programming problems, especially those involving strings or arrays. In this problem, the concept of a counter is used to count the number of consecutive ‘1’s we’ve encountered so far in the string.
These terms and concepts collectively guide us towards an approach that involves scanning the binary string from left to right, maintaining a counter of consecutive ‘1’s, and continuously updating a total count of substrings.
Creating a table or diagram can be beneficial in visualizing the problem and understanding its properties. Let’s take the string “0110111” as an example.
Here’s a table we can create to track the number of substrings that end at each character:
Index  Binary String Character  Number of substrings ending at this index 

0  0  0 
1  1  1 
2  1  2 
3  0  0 
4  1  1 
5  1  2 
6  1  3 
This table reveals a pattern in the problem: The number of substrings ending at an index is either 0 if the character is ‘0’ or one more than the number of substrings ending at the previous index if the character is ‘1’. This pattern informs us about how we can keep track of the number of substrings of ‘1’s as we scan through the binary string from left to right.
The total number of substrings with all characters ‘1’s in the binary string is the sum of the numbers in the third column of this table. In this case, it’s 0 + 1 + 2 + 0 + 1 + 2 + 3 = 9. You can use this table to verify the correctness of your solution.
Remember, this visualization only works for the problem because of the specific properties of binary strings and the fact that we’re looking for substrings with all characters ‘1’s. For different problems, different visualizations may be more appropriate.
How did you infer from the problem statement that this problem can be solved using ?
Simple Explanation of the Proof
I’m having trouble understanding the proof of this algorithm. Could you explain it in a way that’s easy to understand?
Stepwise Refinement
Stepwise Refinement:
a. Initialize a counter and a running sum to zero.
b. Iterate over each character in the binary string.
c. If the character is a ‘1’, increment the counter and add it to the running sum.
d. If the character is a ‘0’, reset the counter to zero.
e. Return the running sum after completing the iteration, taking the modulus by 1e9 + 7 at each step to ensure the answer remains within the required limit.
Granular Steps:
a. Begin by initializing two variables,
count
andtotal
, to zero. Thecount
variable is used to keep track of consecutive ‘1’s in the binary string, and thetotal
variable keeps track of the total number of substrings with all characters ‘1’s.b. Start scanning the binary string from the left. For each character, check if it is a ‘1’ or ‘0’.
c. If the character is a ‘1’, increment
count
by one and then addcount
tototal
.d. If the character is a ‘0’, reset
count
back to zero since we cannot have ‘1’only substrings that include this character.e. While adding
count
tototal
, make sure to take the modulus by 1e9 + 7 to keep the answer within the required range.f. Once the scan of the binary string is complete,
total
will contain the total number of ‘1’only substrings. Return this value.Independent Parts:
The problem does not have independent parts per se, as the count at each step depends on the previous characters in the binary string. However, the process of examining each character can be thought of as a distinct step in the solution.
Repeatable Patterns:
Yes, there are repeatable patterns in our solution. For every ‘1’ character we encounter, we increase the
count
by one and add it tototal
. If we encounter a ‘0’ character, we reset thecount
to zero. These operations are performed repetitively for each character in the binary string.
Solution Approach and Analysis
Let’s break down the approach to solving this problem:
Initialize the Variables: The first step is to initialize two variables,
count
andtotal
, both set to zero. Thecount
variable is used to keep track of the number of consecutive ‘1’s in the binary string, andtotal
is used to store the total number of substrings consisting only of ‘1’s.Iterate through the Binary String: Next, we iterate through each character in the binary string. For each character, we check if it’s a ‘1’ or a ‘0’.
If the Character is ‘1’: If the character is a ‘1’, we increment
count
by 1 and then addcount
tototal
. The increment ofcount
makes sense because we have found another ‘1’ which extends the length of the current substring of ‘1’s. Addingcount
tototal
is because the new ‘1’ can be concatenated to all previous substrings of ‘1’s to form new unique substrings.Suppose we think of this step like making a bead necklace, where each bead represents a ‘1’. Each time we add a bead, we are not just adding a single bead to the total but adding a new combination that includes the new bead and all the combinations we can form with the previous beads.
If the Character is ‘0’: If the character is a ‘0’, we reset
count
back to zero because the series of consecutive ‘1’s is broken, and we start to count a new series.Return the Total Substrings: After we have iterated over every character in the binary string,
total
will contain the total number of substrings that consist only of ‘1’s. We return this value as the final answer. Remember to take modulus by 1e9 + 7 every timetotal
is updated to prevent integer overflow and keep the result within the required range.Changes in Problem Parameters: If the length of the binary string changes, the time complexity of the solution changes linearly, as we need to iterate over each character in the binary string. If the string becomes very long, the solution could become slow. However, the space complexity remains constant, as we are not using any data structures that scale with the size of the input.
Let’s walk through an example:
s = "0110111"
 We start with `count` and `total` both equal to 0.
 The first character is '0', so `count` remains 0.
 The second character is '1'. We increment `count` to 1 and add `count` to `total`, so `total` becomes 1.
 The third character is '1'. We increment `count` to 2 and add `count` to `total`, so `total` becomes 3.
 The fourth character is '0', so we reset `count` to 0.
 The fifth character is '1'. We increment `count` to 1 and add `count` to `total`, so `total` becomes 4.
 The sixth character is '1'. We increment `count` to 2 and add `count` to `total`, so `total` becomes 6.
 The seventh character is '1'. We increment `count` to 3 and add `count` to `total`, so `total` becomes 9.
 Now, `total` (which is 9) is the total number of substrings of '1's in the string, so we return 9 as the output.
Identify Invariant
In the context of this problem, an invariant is a property that remains unchanged during the execution of the algorithm.
The key invariant in this problem is the count
variable, which represents the length of the current run (consecutive sequence) of ‘1’s in the binary string.
Every time we encounter a ‘1’, we increment count
by 1, indicating that we’ve extended the current run of ‘1’s by one. Then, count
is added to total
, the total count of ‘1’s substrings. This operation is based on the observation that a newly encountered ‘1’ can be appended to all substrings ending with the previous ‘1’, thus forming new unique substrings.
However, when we encounter a ‘0’, count
is reset to 0. This is because ‘0’ breaks the continuity of ‘1’s, and we cannot append a ‘0’ to any existing substring of ‘1’s. Hence, we need to start a new count of ‘1’s.
So, regardless of how the string evolves as we iterate through it, the role of the count
variable remains constant: it always represents the current length of consecutive ‘1’s. This property is maintained throughout the algorithm, making count
an invariant in this problem.
Identify Loop Invariant
In this problem, the loop invariant is related to the total number of all ‘1’s substrings we have counted up to the current position in the string during each iteration of the loop.
Let’s denote total
as the total number of ‘1’s substrings, and count
as the current length of consecutive ‘1’s.
The loop invariant here is: At the start of each iteration, total
is equal to the sum of the number of all ‘1’s substrings in the string from the first character up to, but not including, the current character.
Before the loop begins, both total
and count
are initialized to 0. This means no characters have been scanned, and the invariant holds.
During the loop, if the current character is ‘1’, we increment count
by 1 and add count
to total
. So, total
represents the sum of the number of ‘1’s substrings from the first character to the current character inclusive, which is consistent with our loop invariant.
When the current character is ‘0’, count
is reset to 0, but total
remains unchanged. This still maintains our loop invariant, as the total number of ‘1’s substrings doesn’t increase when we encounter a ‘0’.
So, at the start of each iteration, our loop invariant always holds. It is also still true after the loop ends, which means total
represents the total number of all ‘1’s substrings in the entire string, as required by the problem. This shows that our algorithm is correct.
In the context of this problem, the invariant and the loop invariant refer to the same concept. However, it’s important to distinguish between the two terms in a broader context.
An “invariant” is a property of a system that remains unchanged under some transformation or operation. In a programming context, it often refers to a condition that holds true before and after some piece of code is executed.
A “loop invariant,” on the other hand, is a specific type of invariant. It is a condition that is initially true and remains true after each iteration of a loop. It’s a critical tool for proving the correctness of algorithms, especially those involving loops.
So, all loop invariants are invariants, but not all invariants are loop invariants. For this specific problem, the term “invariant” and “loop invariant” are referring to the same property because the invariant condition we’re interested in is specifically related to the loop in our algorithm.
Thought Process
This problem is asking for a way to count all the continuous substrings of 1’s in the given binary string.
Before jumping into the solution, let’s first understand the problem and key insights:
Key Insight: We’re not just counting the individual ‘1’s. We’re counting continuous substrings of ‘1’s of length 1, length 2, length 3, and so on.
Observation: Each new ‘1’ in a continuous substring contributes to a number of new substrings equal to its position in the substring. For example, the third ‘1’ in a string of ‘1’s contributes to three new substrings: itself, the substring of the last two ‘1’s, and the substring of all three ‘1’s.
Given these insights, we can create a solution that counts all the substrings of ‘1’s in the string.
Here’s a Python3 solution using the above strategy:


In this solution, we initialize count
to keep track of continuous ‘1’s and result
to store the total number of substrings. We then iterate through each character in the string s
. If the character is ‘1’, we increment count
and add count
to result
. If we encounter ‘0’, we reset count
to zero. Finally, we return result
modulo 10^9 + 7, which is required by the problem constraints.
Establishing Preconditions and Postconditions
Parameters:
 The method takes one input,
s
. s
is a string.s
represents a binary string, i.e., it consists of only ‘1’s and ‘0’s.
 The method takes one input,
Preconditions:
 Before this method is called, the string
s
must be initialized.  The string
s
should be nonempty and contain only ‘1’s and ‘0’s. Its length should be between 1 and 10^5 inclusive, as per the problem constraints.  The program doesn’t need to be in a specific state to call this method since it doesn’t depend on any global state.
 Before this method is called, the string
Method Functionality:
 The method is expected to return the number of substrings in
s
that contain only ‘1’s. Since the answer may be too large, it should return the result modulo 10^9 + 7.  The method does not change the inputs or the state of the program.
 The method is expected to return the number of substrings in
Postconditions:
 After the method has been called and returned, the program state or values of the parameters are unchanged. The method doesn’t have any side effects.
 The return value represents the total number of substrings in
s
that contain only ‘1’s, modulo 10^9 + 7.
Error Handling:
 The method assumes that the preconditions are always met. There is no specific error handling for when the preconditions are not met. The problem statement specifies that the inputs will always be valid.
 If the input string
s
is empty, or contains characters other than ‘0’ and ‘1’, or exceeds the length limit, the behaviour of the method is not defined.
Problem Decomposition
Problem Understanding:
 The problem is to find the total number of substrings in a given binary string that contain only ‘1’s. If the result is too large, return it modulo 10^9 + 7.
Initial Breakdown:
 The major part of the problem involves scanning the string and identifying the substrings of ‘1’s. This can be broken down into two subproblems:
 Identifying the sequences of ‘1’s.
 Calculating the number of substrings that each sequence of ‘1’s can form.
 The major part of the problem involves scanning the string and identifying the substrings of ‘1’s. This can be broken down into two subproblems:
Subproblem Refinement:
 Identifying sequences of ‘1’s: This involves iterating over the string and keeping a count of consecutive ‘1’s.
 Calculating the number of substrings: The number of substrings in a string of length
n
can be calculated using the formulan*(n+1)/2
.
Task Identification:
 The repeated task here is the calculation of substrings for each sequence of ‘1’s.
Task Abstraction:
 The task of calculating the number of substrings is well abstracted. It’s clear what it does, it is reusable, and it makes sense in the context of the problem.
Method Naming:
 The method that calculates the number of substrings could be named
calculate_substrings
.
 The method that calculates the number of substrings could be named
Subproblem Interactions:
 The task of identifying sequences of ‘1’s should be done first. After each sequence is identified, we immediately calculate the number of substrings it can form. So, these tasks are performed in a sequential and interdependent manner.
From Brute Force to Optimal Solution
A brute force approach to this problem would involve generating all possible substrings of the given string and checking each one to see if it contains only ‘1’s.
Brute Force Approach:
 Initialize a counter to zero.
 Iterate through the string with two nested loops to generate all substrings.
 For each substring, check if it contains only ‘1’s. If it does, increment the counter.
 At the end, return the counter modulo 10^9 + 7.
The above approach is inefficient and has a time complexity of O(n^3), where n is the length of the string. The inefficiency comes from the fact that we’re generating and checking all substrings, even though many of them will contain ‘0’s and thus can’t contribute to our count.
Optimized Approach:
We can make a key observation to optimize our solution: substrings of ‘1’s can only be formed within sequences of consecutive ‘1’s. Therefore, we can simply count the number of substrings within each sequence of ‘1’s and sum them up.
 Initialize a counter for the total number of substrings and a counter for the length of the current sequence of ‘1’s.
 Iterate through the string. For each character:
 If it’s ‘1’, increment the sequence length counter.
 If it’s ‘0’ and the sequence length counter is greater than zero, calculate the number of substrings that can be formed from the sequence of ‘1’s using the formula
n*(n+1)/2
, where n is the sequence length. Add this to the total count, and reset the sequence length counter.
 At the end, if the sequence length counter is greater than zero, calculate the number of substrings for the last sequence of ‘1’s and add it to the total.
 Return the total count modulo 10^9 + 7.
This optimized approach has a time complexity of O(n), which is a significant improvement over the brute force approach. The space complexity is also improved, from O(n^2) for storing all substrings in the brute force approach, to O(1) in the optimized approach, as we’re only storing counts and not actual substrings.
Python Code:


The optimized solution is more efficient and scales better with larger inputs than the brute force solution.
Code Explanation and Design Decisions
Initial Parameters: The initial parameters of our Python function are
s
, which is the input string consisting of ‘0’s and ‘1’s. The significance of this parameter is that it represents the original string we need to analyze to find the count of binary substrings that only contain ‘1’s.Primary Loop: The primary loop in the solution iterates over each character
c
in the input strings
. Each iteration represents an examination of a single character in the context of the current sequence of consecutive ‘1’s. The loop advances the solution by checking each character and updating the count of substrings accordingly.Conditions: The conditional statement within the loop checks whether the current character
c
is ‘1’. If it is, we increment thelength
counter, signifying that we are extending the current sequence of ‘1’s. If the current character is ‘0’, it signifies the end of a sequence of ‘1’s. In this case, we calculate the number of substrings in the sequence using the formulalength*(length+1)//2
, and add this count to the totalcount
.Modifications: In each iteration, we might modify the
length
counter depending on the current character. If the character is ‘1’, we incrementlength
, extending the current sequence. If the character is ‘0’, we resetlength
to zero, preparing for a potential new sequence of ‘1’s. These modifications reflect the changes in the state of the solution with respect to the current sequence of ‘1’s.Invariant: The invariant in this problem is the formula
length*(length+1)//2
. It represents the count of substrings within a sequence of ‘1’s of lengthlength
, and is maintained throughout the code. It helps meet the problem’s objective of counting all substrings of ‘1’s within the given string.Final Output: The final output of the function is the total count of substrings consisting only of ‘1’s, modulo 10^9 + 7, in line with the problem’s requirement. It represents the total number of valid substrings within the original input string
s
, thus satisfying the problem’s requirements.
Coding Constructs
HighLevel Strategies: The primary highlevel problemsolving strategy employed in this code is the usage of a sliding window technique. We slide through the string and track the length of consecutive sequences of ‘1’s, then use that length to calculate the number of substrings within each sequence. It’s essentially a linear scan of the input, with added logic to handle sequence detection and substring counting.
NonProgrammer Explanation: If I were to explain this code to a nonprogrammer, I’d say that it’s like reading a book word by word while counting the number of sentences. If you find a full stop, you calculate how many sentences could have been formed with the words you have seen continuously so far. Each time you encounter a new word after a full stop, you start counting again.
Logical Elements: Independent of any programming language, the logical elements used in this code include a loop for iteration over the characters of the input, a condition to check the current character, a counter to keep track of the length of sequences of ‘1’s, and an accumulator to add up the counts of substrings.
Algorithmic Approach: In plain English, the code scans the input string character by character. If it encounters a ‘1’, it adds one to a running count of consecutive ‘1’s. If it encounters a ‘0’, it calculates the number of substrings that can be formed from the previous sequence of ‘1’s, using the formula
length*(length+1)//2
, wherelength
is the count of consecutive ‘1’s. It then resets thelength
count to zero. This process continues until we’ve examined the whole string.Key Steps: The key steps this code performs are:
 Iteration over the characters of the input string.
 Checking the current character to see if it’s a ‘1’ or ‘0’.
 If it’s a ‘1’, increasing the count of consecutive ‘1’s.
 If it’s a ‘0’, calculating the number of substrings that can be formed from the previous sequence of ‘1’s, adding that count to the total, and resetting the consecutive ‘1’s count.
The purpose of these steps is to find and count all substrings consisting of ‘1’s in the input string.
Algorithmic Patterns: The main algorithmic pattern used in this code is the sliding window technique, which is a common method for handling string and array traversal problems. Here, the “window” is the consecutive sequence of ‘1’s that we’re currently considering, and it “slides” along the input string as we iterate over it.
Language Agnostic Coding Drills
Dissection of Code into Basic Concepts:
Iteration Over a Collection: This is a basic programming concept that involves looping over elements in a collection. In this context, it means looping over the characters of the input string.
Conditional Statements: This is the use of
ifelse
statements to control the flow of the program. Here, we’re checking whether the current character in the iteration is a ‘1’ or ‘0’.Variable Tracking: This involves tracking changes to a variable as the program executes. In this case, it’s maintaining a count of consecutive ‘1’s in the string.
Arithmetic Calculations: Performing mathematical operations to calculate a result. In our case, it’s calculating the number of substrings in a sequence using the formula
length*(length+1)//2
.Accumulation: The process of accumulating a result over the course of iteration, which involves adding the counts of substrings in all sequences of ‘1’s to get the total count.
List of Concepts by Increasing Difficulty:
Iteration Over a Collection: This is one of the most basic concepts in programming. It requires understanding of loops and collections, which are foundational knowledge in most programming languages.
Conditional Statements: This is another basic concept, but it is a bit more complex than iteration because it requires understanding of boolean expressions and control flow.
Variable Tracking: This is more complex because it requires understanding of how variables store and update information, as well as the ability to mentally keep track of changes to a variable as the program executes.
Arithmetic Calculations: This requires understanding of the specific mathematical operations being performed and how they relate to the problem at hand, which makes it a bit more complex than the previous concepts.
Accumulation: This is the most complex of these concepts because it requires understanding of all the previous concepts, as well as the ability to handle intermediate results and use them to calculate a final result.
ProblemSolving Approach:
We start with the problem statement, which asks for the total count of nonempty substrings consisting only of ‘1’s in the given binary string.
We realize that we’ll need to scan the input string to find these substrings. This introduces the concept of iteration over a collection.
While scanning, we’ll need to check each character to see if it’s a ‘1’ or ‘0’. This introduces the concept of conditional statements.
If it’s a ‘1’, we’ll need to keep track of how many ‘1’s we’ve seen in a row. This introduces the concept of variable tracking.
When we reach a ‘0’, we’ll need to calculate the number of substrings that could be formed from the sequence of ‘1’s we’ve just seen, using the formula
length*(length+1)//2
. This introduces the concept of arithmetic calculations.Finally, we’ll need to add this count to a running total so that, by the end of our scan, we have the total count of all such substrings in the string. This introduces the concept of accumulation.
In this way, we use each of these coding drills to solve the problem, with each drill representing a key step in our solution strategy.
Targeted Drills in Python
Python Coding Drills for Each Concept:
Iteration Over a Collection:
1 2 3
def iterate_collection(collection): for element in collection: print(element)
Conditional Statements:
1 2 3 4 5
def check_number(num): if num % 2 == 0: print("Even") else: print("Odd")
Variable Tracking:
1 2 3 4 5 6
def count_numbers(collection): count = 0 for element in collection: if element % 2 == 0: count += 1 print(count)
Arithmetic Calculations:
1 2 3 4 5
def calculate_average(collection): sum_of_elements = sum(collection) number_of_elements = len(collection) average = sum_of_elements / number_of_elements print(average)
Accumulation:
1 2 3 4 5
def sum_elements(collection): total = 0 for element in collection: total += element print(total)
ProblemSpecific Concepts:
In this problem, the specific concept is calculating the number of substrings that can be formed from a sequence of ‘1’s, which is essentially the sum of first
n
natural numbers. Here’s a coding drill for this concept:1 2 3
def calculate_substrings(length): count = length*(length+1)//2 print(count)
This drill is essential for our problem because the final solution involves identifying sequences of ‘1’s in the binary string and calculating the number of substrings that can be formed from each sequence.
Integration of the Drills:
Once all drills have been coded, we can integrate them to solve the initial problem. Here’s a rough breakdown of how they would fit together:
We start by iterating over the binary string. This uses the ‘Iteration Over a Collection’ concept.
For each character, we check if it’s a ‘1’ or ‘0’. This uses the ‘Conditional Statements’ concept.
If it’s a ‘1’, we increase a count. This uses the ‘Variable Tracking’ concept.
If it’s a ‘0’, we calculate the number of substrings that can be formed from the sequence of ‘1’s we’ve just seen and add it to our total. This uses the ‘Arithmetic Calculations’, ‘ProblemSpecific Concept’, and ‘Accumulation’ concepts.
These drills, when assembled together, will form a complete solution to the initial problem. Each drill represents a key concept or step in our solution, and the integration of these drills follows the order and logic of our problemsolving approach.
Q&A
Similar Problems
Here are 10 problems that use similar underlying concepts:
LeetCode 5: Longest Palindromic Substring: Like our problem, this problem requires identifying and handling sequences within a string, although it’s looking for a different kind of sequence (palindromes).
LeetCode 3: Longest Substring Without Repeating Characters: This problem involves iterating through a string and maintaining a count of characters, similar to the way we tracked ‘1’s in our problem.
LeetCode 53: Maximum Subarray: In this problem, you need to find the contiguous subarray with the largest sum, which involves tracking and accumulation, akin to how we tracked sequences of ‘1’s.
LeetCode 76: Minimum Window Substring: This problem requires tracking variables and involves iteration over a string, similar to our problem.
LeetCode 438: Find All Anagrams in a String: Like our problem, this problem involves iterating over a string, maintaining counts of characters, and performing actions based on those counts.
LeetCode 680: Valid Palindrome II: This problem involves identifying a sequence within a string where a condition holds, similar to our identification of sequences of ‘1’s.
LeetCode 11: Container With Most Water: This problem requires tracking variables (here, two pointers) and making calculations based on their positions, somewhat like our tracking of ‘1’s in our problem.
LeetCode 15: 3Sum: This problem requires finding sequences within an array that satisfy certain conditions, similar to how we found sequences of ‘1’s.
LeetCode 485: Max Consecutive Ones: This problem is directly related to our problem, requiring the identification and handling of sequences of ‘1’s within a binary array.
LeetCode 713: Subarray Product Less Than K: This problem involves identifying sequences within an array that satisfy a condition and then performing calculations based on those sequences, which echoes the key steps of our problem.