Quick Overview

A string-processing coding problem: split a sentence on spaces and count the tokens that contain only English letters and the punctuation marks comma, period, question mark and exclamation mark. It tests careful tokenization with repeated spaces and exact character-class rules.

Count Space-Separated Words Made Only of Letters and Basic Punctuation

Company: Visa

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Online Assessment

Count the words in a sentence under a strict definition of a word. Split the sentence on spaces into tokens, discard every token that contains a digit or any character other than an English letter, a comma, a period, a question mark or an exclamation mark, and return the number of tokens that remain. ### Function Signature ```python def count_valid_words(sentence: str) -> int: ``` ### Rules - Tokens are the maximal runs of non-space characters. Leading, trailing and repeated spaces never produce empty tokens. - A token is kept if every one of its characters is an English letter (`a`–`z` or `A`–`Z`) or one of the four punctuation marks `,` `.` `?` `!`. Where the punctuation appears inside the token does not matter. - A token is discarded if it contains a digit, or any other character such as an apostrophe, a hyphen, a bracket, a colon or a quotation mark. - A token made only of the allowed punctuation marks, such as `,` or `!!`, is kept. - Return the number of kept tokens. An empty sentence, or one made only of spaces, gives `0`. ### Constraints - `0 <= len(sentence) <= 10^5` - Every character of `sentence` is printable ASCII (codes 32 to 126), so the space is the only whitespace character. ### Examples **Example 1** ```text Input: sentence = "He has 1 million do[[ars, isnt he?" Output: 5 ``` The tokens are `He`, `has`, `1`, `million`, `do[[ars,`, `isnt` and `he?`. The token `1` contains a digit and `do[[ars,` contains `[`, so both are discarded and five tokens remain. **Example 2** ```text Input: sentence = "Wait!! Is it 9am? Yes, it is." Output: 6 ``` Only `9am?` is discarded, because it contains a digit. **Example 3** ```text Input: sentence = " don't use e-mail , ok " Output: 3 ``` The tokens are `don't`, `use`, `e-mail`, `,` and `ok`. The apostrophe and the hyphen disqualify two tokens; the lone comma is kept because it contains only allowed punctuation.

Overview: A string-processing coding problem: split a sentence on spaces and count the tokens that contain only English letters and the punctuation marks comma, period, question mark and exclamation mark. It tests careful tokenization with repeated spaces and exact character-class rules.

Read the full Visa Software Engineer interview experience this question came from

Count the words in a sentence under a strict definition of a word. You are given a string `sentence`. Split it on spaces into tokens: a token is a maximal run of non-space characters, so leading, trailing and repeated spaces never produce empty tokens. - A token is **kept** if every one of its characters is an English letter (`a`-`z` or `A`-`Z`) or one of the four punctuation marks `,` `.` `?` `!`. Where the punctuation appears inside the token does not matter. - A token is **discarded** if it contains a digit, or any other character such as an apostrophe, a hyphen, a bracket, a colon or a quotation mark. - A token made only of the allowed punctuation marks, such as `,` or `!!`, is kept. Return the number of kept tokens. An empty sentence, or one made only of spaces, gives `0`. The result never exceeds 50,000, so it always fits in a signed 32-bit integer. ### Constraints - `0 <= len(sentence) <= 10^5` - Every character of `sentence` is printable ASCII (codes 32 to 126), so the space is the only whitespace character. ### Example 1 ```text Input: sentence = "He has 1 million do[[ars, isnt he?" Output: 5 ``` The tokens are `He`, `has`, `1`, `million`, `do[[ars,`, `isnt` and `he?`. The token `1` contains a digit and `do[[ars,` contains `[`, so both are discarded and five tokens remain. ### Example 2 ```text Input: sentence = " don't use e-mail , ok " Output: 3 ``` The tokens are `don't`, `use`, `e-mail`, `,` and `ok`. The apostrophe and the hyphen disqualify two tokens; the lone comma is kept because it contains only allowed punctuation.

Constraints

  • 0 <= len(sentence) <= 10^5
  • Every character of sentence is printable ASCII (codes 32 to 126), so the space is the only whitespace character.

Examples

Input: ('',)

Expected Output: 0

Explanation: Minimum valid: an empty sentence has no tokens.

Input: (' ',)

Expected Output: 0

Explanation: Sentence of only five spaces (length 5) produces no tokens; splitting on single spaces would wrongly yield six empty tokens.

Hints

  1. Tokens are the maximal runs of non-space characters: make sure leading, trailing and repeated spaces can never yield an empty token that gets counted.
  2. One character outside the letters and the four marks , . ? ! discards the whole token, wherever in the token it appears.
  3. A token made only of allowed punctuation, such as , or !!, still counts as a word.

Loading coding console...

Show the approach

Approach

Scan the sentence once, character by character, keeping two flags: in_token (we are inside a maximal run of non-space characters) and valid (every character of the current run so far is an ASCII letter a-z / A-Z or one of the four marks , . ? !). A space ends the current run: if a run was open and is still valid, count it, then reset both flags. After the loop the final run, if any, is closed the same way, so a token that ends at the last character is not lost.

Invariant: when a run ends, valid is true exactly when every one of its characters is allowed, so a run is counted if and only if it is a kept token. Each character is tested on its own, so where punctuation sits inside a token does not matter, and a token made only of allowed punctuation (such as , or !!) is counted. A digit or any other printable symbol (apostrophe, hyphen, brackets, colon, quotation marks, @, #, and so on) clears valid for the rest of that token.

Edge cases: only a non-space character opens a run, so leading, trailing and repeated spaces never create an empty token, and an empty or all-space sentence returns 0. The letter test uses explicit ASCII ranges, so the neighbouring symbols @, [, ` and { are rejected. At most 50,000 tokens fit in 10^5 characters, so the count fits in a 32-bit int.

Time complexity:
O(n)
Space complexity:
O(1)