Engineering platform~12 min2s · 256 MBtarget O(n) time, O(n) spacestrings
A build system writes one log line for each step of a build. The on-call page shows the lines as one text, newest line first, with a separator between lines.
The lines arrive oldest first. A busy build has up to 100,000 lines of up to 20 characters. The page is rebuilt every time an engineer opens it, so building the text must stay fast.
Write join_log_lines(lines, separator).
lines[i] is the i-th log line, in the order the lines were written. A line can be empty.separator between each two lines.Input: lines = ["lint", "build", "test", "ship"], separator = ";"
Output: "ship;test;build;lint"The newest line is ship, so it comes first.
Walk the lines from the last to the first, and put the separator before every line except the first one written.
| Place | Line | Separator first? | Text after |
|---|---|---|---|
| 3 | ship | no | ship |
| 2 | test | yes | ship;test |
| 1 | build | yes | ship;test;build |
| 0 | lint | yes | ship;test;build;lint |
Each character is written once, and the text is made once at the end.
Input: lines = ["only"], separator = "|"
Output: "only"One line has no separator at all.
Input: lines = [], separator = ","
Output: ""No lines make an empty text.
0 ≤ lines.length ≤ 100,000 (log lines)0 ≤ lines[i].length ≤ 20, using the characters a to z and 0 to 91 ≤ separator.length ≤ 3, using the characters |, , and ;The text can hold up to about 2,300,000 characters. Putting each line in front of the text so far copies the whole text at every step, which is 114,998,850,000 characters for 100,000 lines of 20 characters. A builder writes each character once, which is about 2,300,000.