Problem overview
The Unix wc command counts things in text. This first version counts lines. The method is countLines(lines: string[]): number, and it returns how many lines the list holds.
For ["hello", "world", "foo", "bar", "baz"] the result is 5. Every entry is one line, even an empty string "", and an empty list has 0 lines.
Approach: the array already knows its size
The input is an array in which each element is a line. The number of lines is the number of elements, and JavaScript arrays keep that number in their length property. No loop and no counter are needed.
A loop that adds 1 for each element would give the same answer, but it repeats work the array already did. The one thing to be careful about is what counts as a line: here every element does, including blank ones. A list such as ["a", "", "b"] has 3 lines. Nothing is skipped or trimmed.
The counting loop looks like this, and it is what length replaces:
let count = 0;
for (const line of lines) {
count++;
}
For the five-line example both give 5. The loop does more work for the same answer, and it adds a variable that has to be started at 0 and kept correct.
Step by step
- Return
lines.length. The property holds the number of entries, so it is the answer as it stands.
The solution in TypeScript
class Solution {
countLines(lines: string[]): number {
return lines.length;
}
}
lengthis a property, so it is written without parentheses. Withlines.length()the compiler reportsThis expression is not callable. Type 'Number' has no call signatures.If the compiled JavaScript runs anyway, it stops withTypeError: lines.length is not a function.- The return type
numbermatches:lengthis always a whole number that is zero or more. - Strings have a
lengthtoo, but it counts characters. Here the argument is an array of strings, solines.lengthcounts the strings andlines[0].lengthwould count the characters of the first one.
This differs from the real wc -l, which counts newline characters in a file. Here the input is already split into lines, so the count is of entries. The two agree when every line, including the last, ends with a newline.
Calling it is a one-liner: new Solution().countLines(["hello", "world", "foo", "bar", "baz"]) returns 5.
The property is always one more than the index of the last entry. The first line is lines[0] and the last is lines[lines.length - 1], so a five-line list has indexes 0 to 4. The value stays current as the array changes: after push adds an entry, length is one larger, with nothing to update by hand.
Complexity
Reading length takes constant time and uses no extra space, however many lines there are.
Edge cases and common mistakes
- Empty list. The result is
0. - Blank lines. They count.
["", ""]has 2 lines, and[""]has 1 line, not 0, even though it holds no text. - Counting characters or words. This version counts lines only. Using
lines.join("").lengthwould count characters instead. - Calling
lengthlike a method. Writelines.length, notlines.length().
Testing
| Input | Result |
|---|---|
["hello", "world", "foo", "bar", "baz"] | 5 |
["a", "", "b"] | 3 |
[] | 0 |
