from typing import Mapping
from typing import Dict


def _group_by_power(name_to_power: Mapping[str, int]) -> Dict[int, str]:
    """
    Groups unit names by the powers that they are raised to.
    For instance, if the input is {"m": 2, "s": 2, "k": 1}, it would output
    {2: "m*s", 1: "k"}.
    :param name_to_power: Maps the name of each unit to its power.
    :return: A dictionary mapping a power to a string representation of the
    products of the units that are raised to this power.
    """
    # Map powers to lists of unit names.
    powers_to_names = {}
    for name, power in name_to_power.items():
        if power not in powers_to_names:
            powers_to_names[power] = []
        powers_to_names[power].append(name)

    # Create combined string representations for each.
    for power, names in powers_to_names.items():
        powers_to_names[power] = "*".join(names)

    return powers_to_names