The real cat command can take many files as arguments and prints them one after another. Build that behavior. Given a list of "files" where each file is itself a list of lines, flatten all lines in order and join them with newlines.
A file can be empty (it adds no lines), and the list can hold no files at all; the result is then an empty string.
Function signature:
public String concatCat(List<List<String>> allLines)
Example:
Input: allLines = [["hello", "world"], ["foo", "bar", "baz"]] Output: "hello\nworld\nfoo\nbar\nbaz" When printed: hello world foo bar baz
Build a flat
List<String> with a nested loop, preserving order. Then return String.join("\n", flat).
List<String> flat = new ArrayList<>();
for (List<String> fileLines : allLines) {
for (String line : fileLines) {
flat.add(line);
}
}
return String.join("\n", flat);
The full solution:
class Solution {
public String concatCat(List<List<String>> allLines) {
List<String> flat = new ArrayList<>();
for (List<String> fileLines : allLines) {
for (String line : fileLines) {
flat.add(line);
}
}
return String.join("\n", flat);
}
}
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
