Problem overview
This is the first version of a small model of the Unix head command. The method head(List<String> lines) returns the first 10 lines joined by newline characters (\n). If the input has fewer than 10 lines, all of them are returned, and an empty list gives an empty string.
For the 13 lines "a" to "m", the result is "a\nb\nc\nd\ne\nf\ng\nh\ni\nj", which prints as the letters a to j, one per row. The letters k, l and m are dropped. The next version lets the caller choose the number of lines.
Approach: cut the list, then join
There are two jobs: pick the lines to keep, then glue them into one string. The second job is String.join("\n", ...), which puts the separator between elements and nothing after the last one. For the first job, lines.subList(0, end) gives the part of the list from index 0 up to, but not including, end.
The only difficulty is choosing end. It should be 10, unless the list is shorter, and then it should be the list's size. That is the smaller of the two numbers, which Math.min computes. With 13 lines it gives 10, with 3 lines it gives 3, and with no lines it gives 0.
Step by step
- Compute the end index. Declare
int end = Math.min(10, lines.size()). It is never more than the number of lines, so the next call can't ask for lines that aren't there. - Cut and join. Take
lines.subList(0, end)and pass it toString.join("\n", ...). Return the result.
The solution in Java
class Solution {
public String head(List<String> lines) {
int end = Math.min(10, lines.size());
return String.join("\n", lines.subList(0, end));
}
}
The Java details worth a closer look:
- The end index is exclusive.
subList(0, 10)holds the elements at positions 0 to 9, which is ten of them. The start is inclusive and the end is not, sosubList(0, end)always has exactlyendelements. - The range must be valid.
subListthrows an exception when the end is larger than the size.Math.minis what prevents that here. - An empty list is fine. With no lines,
endis 0,subList(0, 0)is an empty list, and joining an empty list gives"". subListis a view. It doesn't copy the elements; it looks at the original list. That is fine here, becausejoinreads it immediately, and the method never changeslines.Math.minworks onintvalues. Both arguments here areint(lines.size()returns anint), so no conversion is needed.
Complexity
Time: O(k + C), where k is the number of lines kept (at most 10) and C is their total number of characters. The sublist is created in constant time, and the join copies only the kept lines. Lines beyond the tenth are never touched.
Space: O(C) for the returned string.
Edge cases and common mistakes
- Fewer than 10 lines. Writing
lines.subList(0, 10)directly would throw for a short list. Capping withMath.minreturns all the lines instead. - Exactly 10 and 11 lines. With 10 lines, all of them are returned. With 11, the last one is dropped.
- Empty input. An empty list gives
"", not an error. - A trailing newline. Appending
"\n"after each line in a loop would leave one after the last line.String.joinputs separators only between lines.
Testing
The results below come from running the solution:
- The 13 lines
atomgive"a\nb\nc\nd\ne\nf\ng\nh\ni\nj". - The 3 lines
a,b,cgive"a\nb\nc". - The 11 lines
atokgive"a\nb\nc\nd\ne\nf\ng\nh\ni\nj". - An empty list gives
"".
