Count Valid Space-Delimited Words
Updated
Problem statement
Split text on one or more ASCII space characters and ignore empty pieces. Return the number of valid tokens.
A valid token contains one or more ASCII letters and may optionally end with exactly one of ,, ., ?, or !. Digits are never allowed, and punctuation may not appear anywhere except the final character.
Function
countValidWords(text: String) → intExamples
Example 1
text = "hello world! 123 no-way ok?"return = 3hello, world!, and ok? are valid; the numeric and hyphenated tokens are not.
Example 2
text = " A, b. wow!! fine? "return = 3Repeated spaces are ignored. wow!! is invalid because it has more than one punctuation mark.
Constraints
0 <= text.length <= 100000.textcontains printable ASCII characters.
More Visa Online Assessment questions
- Count Prime Values in SubtreesOA · Seen Nov 2026
- Lock CodeOA · Seen Nov 2026
- Minimum-Sum Tree Fill with Unit StepsOA · Seen Nov 2026
- Longest Non-Decreasing Substring After Pair SwapsOA · Seen Oct 2026
- Count Attack Vectors by Divisor RangeOA · Seen Oct 2026
- Minimum Operations to Reduce a Transaction LedgerOA · Seen Oct 2026
- Earliest Common Meeting StartOA · Seen Oct 2026
- Maximum Index-Weighted Sum with Disjoint SwapsOA · Seen Oct 2026
- Maximum Equalized Substrings After One Adjacent SwapOA · Seen Sep 2026
- Minimum Values with Even Powers of TwoOA · Seen Sep 2026
Similar questions at other companies
- Total Each Letter's Count in an Encoded StringIBM · OAIBM interview questions
- Scale Ingredient QuantitiesUpstart · OAUpstart interview questions
- Validate Email AddressAccenture · OAAccenture interview questions
- Is This Date a Weekend?Salesforce · OASalesforce interview questions
- FIX Message ReconciliationVirtu Financial · OAVirtu Financial interview questions
- IPv4 Address ClassificationRippling · OARippling interview questions
- Generate Table of ContentsJP Morgan Chase · OAJP Morgan Chase interview questions
- Highest-Ranked University by CountryAgoda · OAAgoda interview questions
- Counterfeit CurrencyBox · OABox interview questions
- Convert Snake Case Variables to Camel CaseRamp · OARamp interview questions