#K621. Count Alphabetical Substrings
Count Alphabetical Substrings
Count Alphabetical Substrings
You are given a string s
. Your task is to compute the number of substrings of s
in which the characters appear in non-decreasing alphabetical order.
A substring is a contiguous segment of the string. For example, given s = "abcdf"
, the valid non-decreasing substrings are all substrings of the single segment because the characters are already sorted alphabetically. On the other hand, if the string is not entirely sorted, only some substrings qualify.
Note: If s
is empty, output 0.
The answer should be computed based on the formula for the number of substrings in a contiguous segment of length l, which is \(\frac{l(l+1)}{2}\) (in LaTeX format).
inputFormat
The input consists of a single line containing the string s
. The string may be empty.
outputFormat
Output a single integer which is the total number of non-decreasing alphabetical substrings of s
.
0