


You are given a binary string ‘STR’, containing only zeroes and ones.
Your task is to determine the total number of substrings having only ones.
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the T test cases follow.
The first line of each test case contains a single string, 'STR',
Output Format:
For each test case, print a single line containing a single integer, denoting the total number of substrings containing only ones.
The output for each test case will be printed in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= |STR| <= 10 ^ 5
Where ‘T’ denotes the number of test cases and |STR| represents the total length of the string, ‘STR’.
Time Limit: 1 sec.
1
1101
4
In the given test case, there are 3 required substrings of length 1, at indices, 0, 1, and 3. There is also a required substring of length 2 starting at index 0.
So, there are a total of 4 substrings having only ones.
1
111
6
In the given test case, there are 3 required substrings of length 1, at indices, 0, 1, and 2. There are 2 required substrings of length 2, starting at indices, 0 and 1. There is also a required substring of length 3 starting at index 0.
So, there are a total of 6 substrings containing only ones.
Find out all substrings of the given string and check them individually.
Approach:
The approach is to find all substrings of the given string and then check each substring individually whether it contains all ones or not. We can check this by traversing through the entire string and if any character is zero, then this substring does not contain only ones. But if there is no zero in this substring then this substring contains only ones and thus we can increase the value of our answer by 1.
Steps:
O(N ^ 3), where ‘N’ is the length of the given binary string.
In order to find out all substrings of the given string, we are looping through all indices and then considering all possible lengths of the substrings starting at that index. This takes O(N^2) time. Now, checking whether a substring contains only ones or not takes another O(N) time. So, the overall complexity becomes O(N^3).
O(1).
We are not using any extra auxiliary space.