#C1363. Count Substrings Starting and Ending with One
Count Substrings Starting and Ending with One
Count Substrings Starting and Ending with One
Given a binary string S of length N, your task is to count the number of substrings that start and end with the character '1'. A substring is a contiguous sequence of characters within the string. Note that every occurrence of '1' by itself is considered a valid substring.
The final answer can be computed using the formula:
\(\text{result} = \binom{c}{2} + c = \frac{c \times (c - 1)}{2} + c\)
where \(c\) is the total number of '1's in S.
Example: For S = "10101"
and N = 5
, the total number of valid substrings is 6.
inputFormat
The input consists of two lines:
- The first line contains an integer N, representing the length of the binary string S.
- The second line contains the binary string S composed of the characters '0' and '1'.
outputFormat
Output a single integer — the total number of substrings in S that start and end with '1'.
## sample5
10101
6
</p>