'''
Author: Lluís Vila
ETSEIB - UPC
c 2025-26 
'''

def invert_lang_dict(D):
    solD = {}
    for w, motL in D.items():
        # print(w, motL)
        for mot in motL:
            if mot not in solD:
                solD[mot] = [w]
            else:
                solD[mot].append(w)
    
    for k in solD:
        solD[k].sort()
        # solD[k] = tuple(solD[k])

    return solD

def invert_lang_dict_plus(D):
    solD = {}
    for w, motL in D.items():
        # print(w, motL)
        for mot in motL:
            inicial = mot[0]
            if inicial not in solD:
                solD[inicial] = {mot: [w]}
            else:
                lD = solD[inicial]
                if mot not in lD:
                    lD[mot] = [w]
                else:
                    lD[mot].append(w)
    
    for k in solD:
        for inicial in solD[k]:
            solD[k][inicial].sort()
        # solD[k] = tuple(solD[k])

    return solD
