Count Words
Easy · rating 950 · Strings
Read a single line. Print the number of whitespace-separated words it contains.
Editorial
Count Words returns the number of whitespace-separated words in a line. Splitting on whitespace — which collapses runs of spaces — and taking the length is the cleanest approach.
print(len(line.split()))Complexity
Time: O(n). Space: O(n).
Related problems
- Capitalize Words — Easy
- Character Count — Easy
- Count Uppercase Letters — Easy
- Distinct Words — Easy
- Product of Digits — Easy
- Reverse a String — Easy