Problem overview
The Unix wc command counts things in a text. Its -l flag counts lines. This first version does only that: CountLines receives a List<string>, where each entry is one line, and returns how many lines there are as an int.
For ["hello", "world", "foo", "bar", "baz"] the answer is 5. Every entry counts as a line, even an empty string "", because a blank line is still a line. An empty list has 0 lines.
Approach
Real wc -l counts the newline characters in a file. This lesson works one level up: the text has already been split into lines, so the number of lines is the number of entries in the list. There is no splitting or searching left to do.
You could write a loop with a counter and add one for every entry. That works, but a List<T> already keeps track of how many items it holds. Reading that number is a single operation, so no loop is needed.
The same reasoning covers the special cases. A list with one empty string has one entry, so the answer is 1. An empty list has no entries, so the answer is 0. Nothing in the method has to treat them differently.
Step by step
- Return the list's
Count. It is a property, so you read it without parentheses. The method's return type isint, andCountis anint, so no conversion is needed.
The solution in C#
class Solution {
public int CountLines(List<string> lines) {
return lines.Count;
}
}
Details worth knowing:
CountorLength? AList<T>hasCount. An array (string[]) and astringhaveLength. Writinglines.Lengthon a list is a compile error that beginserror CS1061: 'List<string>' does not contain a definition for 'Length'. The names differ because a list can grow and shrink, so it reports how many items it holds right now, while an array's size is fixed when it is created.- The LINQ method
Count(). Withusing System.Linq;,lines.Count()also works and returns the same number. For a list it simply reads theCountproperty, so the property is the more direct choice and needs no extrausing. - It counts entries, not characters.
lines.Countsays nothing about how long each line is. The list["", "", ""]has three entries and no characters at all. - A
nulllist fails. Iflineswerenull, readingCountwould throw aNullReferenceException. The lesson always passes a real list, possibly an empty one. List<T>comes fromSystem.Collections.Generic. In a file of your own you would needusing System.Collections.Generic;at the top.
Complexity
Time: O(1). The list stores its item count, so reading it doesn't depend on the size of the list.
Space: O(1). The method creates no new data.
Edge cases and common mistakes
- Skipping blank lines. Counting only non-empty strings would turn
["a", "", "b"]into 2, but the answer is 3. - Using
Length. It doesn't compile for a list. UseCount. - Looping for nothing. A
foreachwith a counter gives the same result, only with more code. Prefer the property. - Counting newline characters. The entries in this list don't contain
\n. The list already holds the lines separately, so the number of entries is the number of lines.
Testing
["hello", "world", "foo", "bar", "baz"]gives5.[]gives0.[""]gives1.["a", "", "b"]gives3.
