Ctrl K

Chunks Between Spaces

Easy
You are given a string line. A chunk is a maximal run of consecutive non-space characters. Return the number of chunks in the line. Spaces may come in runs, and the line may start or end with spaces.
Example 1
line="  ok,  let's   go! " → 3

The space-free chunks are ok,, let's and go!. Extra spaces at the edges and in between change nothing.

Example 2
line="2+2=4" → 1

There are no spaces, so the whole line is one chunk.

Example 3
line="   " → 0

Only spaces, so there are no chunks.

Example 4
line="" → 0
Constraints
  • 0 ≤ len(line) ≤ 300
  • line consists of English letters, digits, spaces and the characters !@#$%^&*()_+-=',.:
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValue of a Roman Numeral EasyShared Start of Words EasyBracket Balance EasyWhere the Fragment Starts EasyLength of the Final Word Easy
loading Python…
Input
line="  ok,  let's   go! "
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 6 tests on the server, hidden ones included.