Approach #1: Insert Each Character [Accepted]
We can write out each character in the string
S one by one.
As we write characters, we can update
(lines, width) that keeps track of how many lines we have used, and what is the length of the used space in the last line.
If the space
w of the next character in
S fits our current line, we will add it. Otherwise, we will start a new line, and use
w space to put that character on the next line.
Time Complexity: , as we iterate through
Space Complexity: additional space, as we only use
width. (In Java, our
toCharArraymethod makes this , but we could use
Analysis written by: @awice.