The Count Substrings with Only One Distinct Letter coding problem asks you to find the total number of substrings within a given string that consist of only one unique character. For example, in the string "aaa", the valid substrings are "a" (three times), "aa" (twice), and "aaa" (once), totaling 6.
This is a classic problem of identifying contiguous blocks of identical characters and calculating how many smaller pieces can be carved out of them.
Companies like Virtu Financial use this question to test a candidate's ability to apply mathematical shortcuts to string processing. While you could generate all substrings and check them individually, doing so would be highly inefficient. This problem evaluates whether you can recognize the math interview pattern relating to triangular numbers and apply it to a string interview pattern task.
This problem is solved using Linear Scan and Combinatorics.
s = "aabbb"
The substrings for "aa" are s[0], s[1], and s[0..1].
The substrings for "bbb" are s[2], s[3], s[4], s[2..3], s[3..4], and s[2..4].
Whenever you encounter a problem involving "consecutive identical elements," think about grouping them into counts first. Many subarray and substring problems become trivial math problems once you know the lengths of these uniform blocks.