A mosaic restorer is re-cataloguing a single row of n tiles running along a museum corridor. Each tile is glazed in one of 26 shades, recorded left to right as a lowercase string s. A stretch of the row (a substring, identified by its start and end position) is monochrome if every tile inside it shares the same shade.
Count how many monochrome stretches appear in the row, counting each (start, end) position pair separately even when two stretches show the identical run of shades.
A single integer: the number of substrings of s (by start/end position) in which every character is identical.
Example 1
Input
aaa
Expected
6
Explanation
s = "aaa" is one run of length 3. The monochrome stretches are the 3 single tiles, the 2 length-2 stretches ("aa" at 0-1 and 1-2), and the 1 length-3 stretch ("aaa"), giving 3+2+1 = 6.
Example 2
Input
aabba
Expected
7
Explanation
s = "aabba" splits into runs "aa" (length 2), "bb" (length 2) and "a" (length 1). Each run of length L contributes L*(L+1)/2 monochrome stretches: 2*3/2=3 for "aa", 2*3/2=3 for "bb", and 1*2/2=1 for the final "a", totaling 3+3+1 = 7.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →