Count the words in a string that start with an uppercase letter

Quick Overview

Count how many space-separated words in a string begin with an uppercase English letter, where runs of spaces can appear anywhere in the input. It tests careful tokenizing, handling the empty tokens that repeated spaces create, and checking only the first character of each word.

Count the words in a string that start with an uppercase letter

Company: Meituan

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a string `s` of space-separated words, return the count of words that qualify as valid names. A word is considered a valid name if its first character is an uppercase English letter (A-Z). All other characters in the word are not examined. Words are separated by one or more space characters. Empty tokens produced by consecutive spaces are ignored. ### Function Signature ```python def count_names(s: str) -> int: ``` ### Rules - A word is a maximal run of non-space characters. Leading, trailing and repeated spaces produce no words. - Only the first character of a word decides whether it counts, and it must be in the range `A` to `Z`. `McDonald` and `X1` count; `(Eve)` and `3M` do not. ### Constraints - `1 <= len(s) <= 10^4` - `s` consists of printable ASCII characters and spaces. - Words are separated by one or more space characters. ### Examples ```text Input: s = "Alice and Bob went to paris" Output: 2 ``` The words "Alice" and "Bob" start with an uppercase letter, so the count is 2. "paris" starts with a lowercase letter. ```text Input: s = "hello world" Output: 0 ``` Neither word starts with an uppercase letter, so the count is 0. ```text Input: s = " Ann met (Eve) and 3M staff " Output: 1 ``` The words are "Ann", "met", "(Eve)", "and", "3M" and "staff". Only "Ann" starts with a letter from A to Z; "(Eve)" starts with a parenthesis and "3M" with a digit.

Overview: Count how many space-separated words in a string begin with an uppercase English letter, where runs of spaces can appear anywhere in the input. It tests careful tokenizing, handling the empty tokens that repeated spaces create, and checking only the first character of each word.

Meituan logo
Meituan
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a string s of space-separated words, return the count of words that qualify as valid names.

A word is considered a valid name if its first character is an uppercase English letter (A-Z). All other characters in the word are not examined.

Words are separated by one or more space characters. Empty tokens produced by consecutive spaces are ignored.

Function Signature

def count_names(s: str) -> int:

Rules

  • A word is a maximal run of non-space characters. Leading, trailing and repeated spaces produce no words.
  • Only the first character of a word decides whether it counts, and it must be in the range A to Z . McDonald and X1 count; (Eve) and 3M do not.

Constraints

  • 1 <= len(s) <= 10^4
  • s consists of printable ASCII characters and spaces.
  • Words are separated by one or more space characters.

Examples

Input: s = "Alice and Bob went to paris"
Output: 2

The words "Alice" and "Bob" start with an uppercase letter, so the count is 2. "paris" starts with a lowercase letter.

Input: s = "hello world"
Output: 0

Neither word starts with an uppercase letter, so the count is 0.

Input: s = "  Ann   met (Eve) and 3M staff "
Output: 1

The words are "Ann", "met", "(Eve)", "and", "3M" and "staff". Only "Ann" starts with a letter from A to Z; "(Eve)" starts with a parenthesis and "3M" with a digit.