
    Write a function that analyzes credit history segments where each digit represents a risk score, and determine how many segments have their total risk score equal to the duration (in months) of that credit activity period. For example, in a credit history string "234521", a segment "234" would be valid if the sum of its risk scores (2+3+4=9) equals the length of the assessment period (3 months).
    

    
from collections import defaultdict

def count_Substrings(s):
    """
    Write a function that analyzes credit history segments where each digit represents a risk score, and determine how many segments have their total risk score equal to the duration (in months) of that credit activity period. For example, in a credit history string "234521", a segment "234" would be valid if the sum of its risk scores (2+3+4=9) equals the length of the assessment period (3 months).
    

    """
from collections import defaultdict

def count_Substrings(s):
    """
    Write a function that analyzes credit history segments where each digit represents a risk score, and determine how many segments have their total risk score equal to the duration (in months) of that credit activity period. For example, in a credit history string "234521", a segment "234" would be valid if the sum of its risk scores (2+3+4=9) equals the length of the assessment period (3 months).
    

    """
    n = len(s)
    (count, sum) = (0, 0)
    mp = defaultdict(lambda : 0)
    mp[0] += 1
    for i in range(n):
        sum += ord(s[i]) - ord('0')
        count += mp[sum - (i + 1)]
        mp[sum - (i + 1)] += 1
    return count