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.