Problem · String
Implement tail -n
Learn this problemProblem statement
You are given a string fileContents that represents the contents of a text file. Lines are separated by newline characters
.
Return the last n lines of the file in their original order. If the file has fewer than n lines, return the entire contents. If n = 0, return an empty string.
Function
tailLastNLines(n: int, fileContents: String) → String[]Complete the function tailLastNLines in the editor below.
tailLastNLines has the following parameters:
int n: the number of trailing lines to keepString fileContents: the full file contents
Returns
String[]: the last n lines in order, without adding extra formatting.
Examples
Example 1
n = 2fileContents = "a\nb\nc\n"return = ["b", "c"]The last two lines are b and c.
Example 2
n = 0fileContents = "a\nb\nc"return = []Keeping zero trailing lines returns an empty array.
Constraints
0 <= nfileContentsmay be large, so an efficient solution should avoid unnecessary copies.