
    Write a function that analyzes a customer's transaction risk sequence to identify periods of balanced financial behavior, where a period is considered balanced if the sum of its risk scores equals the duration of that period. Given a string of transaction risk scores (0-9), determine how many such balanced periods exist in the customer's financial history.
    

    
from collections import defaultdict

def count_Substrings(s):
    """
    Write a function that analyzes a customer's transaction risk sequence to identify periods of balanced financial behavior, where a period is considered balanced if the sum of its risk scores equals the duration of that period. Given a string of transaction risk scores (0-9), determine how many such balanced periods exist in the customer's financial history.
    

    """
from collections import defaultdict

def count_Substrings(s):
    """
    Write a function that analyzes a customer's transaction risk sequence to identify periods of balanced financial behavior, where a period is considered balanced if the sum of its risk scores equals the duration of that period. Given a string of transaction risk scores (0-9), determine how many such balanced periods exist in the customer's financial history.
    

    """
    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