Problem overview
CountChars takes a List<string> of lines and returns the total number of characters in them. This is a simplified take on the character count of wc: only the characters inside each line count, not the newlines that would separate the lines in a file. Spaces and tabs are characters like any other, one each.
For ["hello", "world"] the answer is 5 + 5 = 10. An empty line adds 0, and an empty list gives 0.
Approach
The answer is the sum of the line lengths. A string in C# knows its own length, so there is nothing to scan or split: visit each line, take its Length, and add it to a running total.
The problem is small on purpose. It sets up the loop-and-accumulate pattern that the next lesson, which combines all the wc counts, reuses.
Step by step
- Create
total. Anintthat starts at 0, before any line has been looked at. - Write a
foreachloop overlines, with each entry namedline. A list can be walked this way without an index. - Add
line.Lengthtototalinside the loop. Every line contributes its own length, spaces and tabs included. - Return
totalafter the loop.
The solution in C#
class Solution {
public int CountChars(List<string> lines) {
int total = 0;
foreach (string line in lines) {
total += line.Length;
}
return total;
}
}
The details worth knowing:
Lengthwithout parentheses. On astringand on an array,Lengthis a property. On aListthe size isCount. Writingline.Length()is a compile error.Lengthcounts UTF-16 code units. For plain ASCII text, which is all this lesson uses, that is the same as the number of characters. Some characters outside ASCII can take two units, which is why the statement restricts the input.- Tabs are one character.
"a\tb"has length 3: the\tin the source is a single tab character. - Strings are immutable. Reading
Lengthnever changes the line.
The same sum with LINQ
C# can also express the loop as one expression. With using System.Linq;, lines.Sum(l => l.Length) returns the same total, and it returns 0 for an empty list. The explicit loop is what the lesson builds, and it is the clearer way to see how a running total works, which is why the steps use it. Both versions read each line once.
The variable total is a plain int. It can hold values up to about 2.1 billion, far more than a list of text lines will add up to in the lesson's tests, and if it did overflow, C# would wrap around silently rather than report an error.
Complexity
Time: O(n), where n is the number of lines. Reading a string's length takes constant time, because the string stores it, so the cost does not depend on how long the lines are. Space: O(1), a single integer.
Edge cases and common mistakes
- Adding the newlines. The convention here is not to count them. The example would then report 11 instead of 10 (one newline between two lines).
- Trimming the line first. Spaces count in this lesson, so call
Lengthon the line as it is. - Counting lines instead of characters.
lines.Countis the number of lines, 2 for the example. - An empty list or empty lines. Both add 0, with no special case.
Testing
["hello", "world"]gives10.["a b", "", "\t"]gives4.[]gives0.
