def add_newlines_by_spaces(string, line_length):

    """Return a version of the passed string where spaces (or hyphens) get replaced with a new line if the accumulated number of characters has exceeded the specified line length"""

    replacement_indices = set()
    current_length = 0

    for i, char in enumerate(string):
        current_length += 1
        if current_length > line_length and (char == " " or char == "-"):
            replacement_indices.add(i)
            current_length = 0

    return "".join([("\n" if i in replacement_indices else char) for i, char in enumerate(string)])