Problem overview
This is the first version of a small command-line tool, a miniature of the Unix cat command. The method cat receives a List<String> of lines and returns one String in which the lines are separated by newline characters (\n), so that printing the result shows one line per row.
For the list ["hello", "world", "foo"] the result is the string hello\nworld\nfoo, which prints as:
hello
world
foo
Blank lines count as lines, and an empty list gives an empty string. The next version builds on this one.
Approach: join, don't append
A first idea is a loop that appends each line followed by "\n". That leaves a newline after the last line, which the example doesn't have, so the loop needs a special case for the end (or for the start). The word to look for is separator: a newline goes between two lines, never after the last one. Java's standard library already has a method for exactly that.
String.join(separator, elements) is a static method that glues the elements together with the separator between each pair. It accepts any Iterable of character sequences, so a List<String> works as it is, with no loop and no special cases.
Step by step
- Return the joined lines. Call
String.join("\n", lines)and return the result. The first argument is the separator and the second is the collection to join.
That is the whole lesson. The method has one statement, because the library does the looping.
The solution in Java
class Solution {
public String cat(List<String> lines) {
return String.join("\n", lines);
}
}
The Java details worth a closer look:
- The separator is only put between elements. With one element there is nothing to separate, so
["only"]givesonlywith no newline. With an empty list, there is nothing to join and the result is the empty string. "\n"is a single character. In a Java string literal,\nis an escape sequence for the newline character. The string"hello\nworld"has 11 characters, not 12.- Static call.
String.joinbelongs to theStringclass, not to a particular string, so it is called on the class name. - The input is not changed.
joinbuilds a new string and leaves the list as it was.
Complexity
Time: O(total length). Every character of every line is copied once into the result, plus one separator between each pair of lines.
Space: O(total length) for the returned string. No other collection is built.
Edge cases and common mistakes
- A trailing newline. A loop that appends
line + "\n"for every line returnshello\nworld\nfoo\n, which differs from the expected result.String.joinavoids this. - Blank lines.
["a", "", "b"]givesa\n\nb. The empty string is a real line, so both newlines stay. - An empty list against a list with one blank line. Both give the empty string,
"". A list of two blank lines,["", ""], gives a single newline,"\n". Joining only inserts separators, so the result alone can't tell those first two inputs apart. - Swapped arguments. The separator comes first.
String.join(lines, "\n")is a compile error: nojoinmethod accepts aList<String>as its first argument.
Testing
The results below come from running the solution:
["hello", "world", "foo"]gives"hello\nworld\nfoo".["a", "", "b"]gives"a\n\nb".["only"]gives"only".[]gives"".["", ""]gives"\n".
