
    Given a user's social interaction pattern represented as a numeric string, find the number of connection segments where the sum of interaction counts equals the segment length, indicating optimal friend group sizes. Each digit in the pattern represents the number of mutual connections within that network segment, and a valid segment is one where the total connections match the group size.
    

    
from collections import defaultdict

def count_Substrings(s):
    """
    Given a user's social interaction pattern represented as a numeric string, find the number of connection segments where the sum of interaction counts equals the segment length, indicating optimal friend group sizes. Each digit in the pattern represents the number of mutual connections within that network segment, and a valid segment is one where the total connections match the group size.
    

    """
from collections import defaultdict

def count_Substrings(s):
    """
    Given a user's social interaction pattern represented as a numeric string, find the number of connection segments where the sum of interaction counts equals the segment length, indicating optimal friend group sizes. Each digit in the pattern represents the number of mutual connections within that network segment, and a valid segment is one where the total connections match the group size.
    

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