Problem overview
The second version of the head tool takes the number of lines as a parameter: head(List<String> lines, int n). It returns the first n lines joined by \n. In this version n is promised to be between 0 and lines.size(), so the method needs no checks. When n is 0, the result is the empty string.
For ["alpha", "beta", "gamma", "delta", "epsilon"] and n = 3, the result is "alpha\nbeta\ngamma".
Approach: replace the constant with n
The previous version cut a fixed 10 lines from the list. Now the caller decides how many, so the end index of the cut is simply n. Because n never goes past the size of the list, the cut is always valid, and the whole method is one statement: take lines.subList(0, n) and join it with "\n".
A loop that appends the first n lines one by one would also work, but it would need the separator logic again: a newline between lines and none at the end. subList and String.join already handle both, so the method states what it wants (the first n lines, joined) instead of how to build it.
Step by step
- Cut and join. Pass
lines.subList(0, n)toString.join("\n", ...)and return the result. The sublist holds positions 0 ton - 1, which are the firstnlines.
Nothing else is needed, because the lesson's rule keeps n inside the valid range.
The solution in Java
class Solution {
public String head(List<String> lines, int n) {
return String.join("\n", lines.subList(0, n));
}
}
The Java details worth a closer look:
- The range is checked when
subListis called. A list of 5 lines withn = 6throws anIndexOutOfBoundsException(its message istoIndex = 6), and a negativensuch as-1throws anIllegalArgumentException. Neither case occurs here, because of the promise aboutn. The following versions deal with both. - Zero is valid.
subList(0, 0)is an empty list, andString.jointurns it into"". This also covers an empty input list withn = 0. - Method parameters. The method has the same name as the one in version 1 but takes one more parameter,
int n. In Java, a method's name plus its parameter types identify it. nequal to the size.subList(0, lines.size())is allowed: the end index is exclusive, so it may equal the size. All the lines are returned.
Take n = 3 on the example. The sublist is [alpha, beta, gamma], the list of positions 0, 1 and 2. Joining it with a newline gives alpha, a newline, beta, a newline and gamma, with nothing after the last word. delta and epsilon at positions 3 and 4 are never read.
Complexity
Time: O(n + C) in the worst case, where C is the number of characters in the kept lines: the sublist is a constant-time view, and the join copies the n lines kept.
Space: O(C) for the returned string.
Edge cases and common mistakes
n = 0. The result is"", with no exception.nequal to the number of lines. All five lines of the example come back forn = 5.- Off-by-one on the end index.
subList(0, n - 1)returns one line too few, andsubList(0, n + 1)one too many, or an exception whennequals the size. - Using the lines' count instead of
n. Passinglines.size()as the end ignores the parameter and returns every line.
Testing
The results below come from running the solution on ["alpha", "beta", "gamma", "delta", "epsilon"]:
n = 3gives"alpha\nbeta\ngamma".n = 0gives"".n = 5gives"alpha\nbeta\ngamma\ndelta\nepsilon".
An empty list with n = 0 also gives "".
