Problem overview
This version of wc counts characters. The function countChars takes a vector<string> of lines and returns the total number of characters as an int. Only the characters inside the lines count. The newline characters that would separate the lines in a file are not counted, and spaces and tabs inside a line count as one character each. The input is plain ASCII text.
For ["hello", "world"] the answer is 10, because each word has 5 characters. An empty line adds 0, and an empty vector gives 0. The real wc counts differently, since it also counts newlines and has separate modes for bytes and characters, but here the answer is simply the sum of the line lengths.
Approach: add up the sizes
This is the same running-total pattern as the word count from the previous version, but easier, because nothing has to be split. A string already knows its length, and size() returns it in constant time. So the answer is the sum of lines[i].size() over all positions.
Step by step
- Create
totalas anintset to 0. A running total starts at zero, before any line is counted. - Loop over the lines by position with
ifrom 0 whilei < lines.size(). - Add
lines[i].size()tototal. This is the only line of work in the loop: each line contributes its own length, spaces and tabs included. - Return
totalafter the loop. The return runs once, when every line has been added.
The solution in C++
class Solution {
public:
int countChars(vector<string> lines) {
int total = 0;
for (int i = 0; i < lines.size(); i++) {
total += lines[i].size();
}
return total;
}
};
The C++ details worth a closer look:
size()countscharvalues, which are bytes. For ASCII text that is the same as the number of characters. A letter outside ASCII, such as an accented one, usually takes several bytes in UTF-8, sosize()would count more than one for it. That is why the lesson restricts the input to ASCII.size()is unsigned. It returnssize_t. The linetotal += lines[i].size()mixes it with anintand the result is converted back tointwhen stored. That is harmless for any realistic amount of text, and the lesson's tests stay far below the limits of anint.size()andlength()are the same. Both return the number of characters in astring;size()matches the name used byvectorand the other containers.- Escape sequences are one character. In a C++ string literal,
"\t"is a single tab character, so"\tx"has size 2.
Complexity
Time: O(n), where n is the number of lines. Each iteration calls size(), which does not look at the characters. The by-value parameter makes the call copy the whole vector, which costs time proportional to the total length, but the counting itself does not.
Space: O(1) extra besides that copy: one integer for the total and one for the loop counter.
Edge cases and common mistakes
- Empty vector. The loop doesn't run and the function returns 0.
- Empty lines.
["", ""]gives 0, because each empty line has size 0. - Whitespace counts.
["a b", "\tx"]gives 5: three characters in the first line (the space is one) and two in the second (the tab is one). - Counting newlines. Adding 1 per line for a separator gives a different number than the lesson wants. The answer is the sum of the line lengths and nothing else.
- Counting words instead. Reusing the stream-based loop from the previous version counts words, not characters.
- Resetting the total in the loop. Declaring
totalinside the loop would reset it on every line, and the declaration would not be visible at thereturn.
Testing
["hello", "world"]gives10.["a b", "\tx"]gives5.["", ""]gives0.[]gives0.
