Math Solution

class Solution:
    def countLetters(self, s: str) -> int:
        """ tc O(N) sc O(1)
        main idea: counting consecutive substring share one character,  get combination of occurance (1+n)*n//2 
        """
        n = len(s)
        cnt = 1
        res = 0
        for i in range(1,n)
            if s[i] == s[i-1]:
                cnt += 1 
            else:
                res += (1+cnt)*cnt // 2
                cnt = 1
        res += (1+cnt)*cnt //2 
        return res 

DP solution

class Solution:
    def countLetters(self, s: str) -> int:
        """ tc O(N) sc O(1)
        main idea: counting consecutive substring share one character, if yes accumutively increment  cnt  by 1. Otherwise, reset cnt to 1 
        each iteration update res with cnt 
        note, res start with 1 
        """
        n = len(s)
        cnt = 1
        res = 1
        for i in range(1,n):
            if s[i] == s[i-1]:
                cnt += 1 
            else:
                cnt = 1
            res += cnt 
        return res