from collections import defaultdict


def gen_counts(residue_contacts):
    """
    Computer interaction-counts for each residue pair.

    For example:
        gen_counts([(0, 'Ligand', 'R110'), (0, 'Ligand', 'N108'), (1, 'Ligand', 'R110')])
        #  => { ("Ligand", "R110"): 2, ("Ligand", "N108"): 1 }

    Parameters
    ----------
    residue_contacts: list[tuple[int, str, str]]
        Residue interactions specified by tuple of frame and two residue ids

    Returns
    -------
    dict of (str, str): int
        Mapping of residue-residue interactions to frame-count
    """
    rescontact_frames = defaultdict(set)
    for frame, res1, res2 in residue_contacts:
        rescontact_frames[(res1, res2)].add(frame)

    rescontact_counts = {(res1, res2): len(frames) for (res1, res2), frames in rescontact_frames.items()}
    return rescontact_counts