Problem statement

https://binarysearch.com/problems/Number-of-Unique-Character-Substrings/

Solution

Equal to Leetcode 1180 Count Substrings with Only One Distinct Letter.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def solve(self, s):
        lens = [len(list(j)) for i, j in groupby(s)]
        return sum(i*(i+1)//2 for i in lens)