Problem overview
The real cat prints several files one after another. Version 4 models that with a function concatCat(allLines: string[][]): string. Each entry of allLines is one "file", itself an array of lines. The function puts all the lines in order, first file first, and joins them with newlines.
For [["hello", "world"], ["foo", "bar", "baz"]] the result is "hello\nworld\nfoo\nbar\nbaz". A file can be empty and adds no lines. With no files at all the result is an empty string.
Approach: flatten, then join
The job has two parts. First turn the array of arrays into one flat array of lines, keeping the order. Then join that array with "\n", exactly as in version 1.
Flattening needs a loop inside a loop. The outer loop visits each file, and the inner loop visits each line of that file and pushes it onto a flat array. Because the outer loop goes from the first file to the last, and the inner one from the first line to the last, the order comes out right.
Joining once at the end matters: it puts newlines between lines, including the last line of one file and the first line of the next, and leaves none at the very end.
Step by step
- Create an empty array
flatof typestring[]. It will hold every line of every file and has no arrays inside it. - Loop over the files with
ifrom 0 whilei < allLines.length. HereallLines[i]is one file, an array of lines. - Inside it, loop over the lines of that file with
jfrom 0 whilej < allLines[i].length. - Push
allLines[i][j]ontoflat. An empty file never enters the inner loop, so it adds nothing. - After both loops, return
flat.join("\n"). This line sits outside the loops' braces, so it runs once after every file has been read.
The solution in TypeScript
class Solution {
concatCat(allLines: string[][]): string {
let flat: string[] = [];
for (let i = 0; i < allLines.length; i++) {
for (let j = 0; j < allLines[i].length; j++) {
flat.push(allLines[i][j]);
}
}
return flat.join("\n");
}
}
string[][]means an array whose elements are arrays of strings.allLines[i][j]reads the line at positionjof filei.- The built-in
allLines.flat()does the flattening in one call, and[[1, 2], [3]].flat()gives[1, 2, 3]. The nested loops show what that call does, and they work the same way. - The inner loop reads
allLines[i].lengthfor the current file, so files of different lengths are fine.
Complexity
If there are m files with L lines in total, the loops make m + L steps and the join copies every character once. Time is O(m + total length). Space is O(L) for flat, plus the result string.
Edge cases and common mistakes
- No files.
[]gives"". - Empty files.
[[], ["a"], [], ["b", "c"]]gives"a\nb\nc". Empty files add no lines and no extra newlines. - A file with one empty line.
[[""], [""]]has two lines, both blank, so the result is"\n": one newline between two empty lines. - Mixing up the indexes. Writing
allLines[j][i], or usingifor both loops, reads the wrong element or goes past the end of an array. - Joining inside the loop. It would either put the newline in the wrong places or leave one at the end. Join once, after the loops.
Testing
| Input | Result |
|---|---|
[["hello", "world"], ["foo", "bar", "baz"]] | "hello\nworld\nfoo\nbar\nbaz" |
[[], ["a"], [], ["b", "c"]] | "a\nb\nc" |
[[""], [""]] | "\n" |
[] | "" |
