from typing import Set


def _unique_name(name: str, preexisting_particles: Set[str]) -> str:
    """Create a string that does not exist in preexisting_particles based on name.

    Args:
        name: Original name
        preexisting_particles: Names that the particle cannot have as name.

    Returns:
        name: Will be `name` if `name` is not in preexisting_particles or of the format "name [i]" where i
            begins at 0 and increases until the name is not preexisting_particles.
    """
    if name not in preexisting_particles:
        preexisting_particles.add(name)
        return name

    name += " [0]"
    i = 1
    while name in preexisting_particles:
        name = name[: name.rfind("[")] + f"[{str(i)}]"
        i += 1
    preexisting_particles.add(name)
    return name