def urlify(string, length):
    """ Replace single spaces with %20 and remove trailing spaces
    Time complexity: O(N)
    Space complexity: O(1): in-place
    """
    new_index = len(string)  # This is the actual length including trailing spaces to hold the additional character
    for i in reversed(range(length)):  # It's easiest to modify strings by going from the end of the string to the beginning
        if string[i] != ' ':
            string[new_index - 1] = string[i]  # Shift character to the end of string
            new_index -= 1
        else:
            string[new_index - 3:new_index] = '%20'  # Replace spaces
            new_index -= 3
    return string