A sentence consists of lowercase letters ('a' to 'z'), digits ('0' to
'9'), hyphens ('-'), punctuation marks ('!', '.', and ','), and spaces (' ') only. Each sentence can be broken down into one or more tokens separated by one or more spaces ' '.
A token is a valid word if all three of the following are true:
It only contains lowercase letters, hyphens, and/or punctuation (no digits).
There is at most one hyphen '-'. If present, it must be surrounded by lowercase characters ("a-b" is valid, but "-ab" and "ab-" are not valid).
There is at most one punctuation mark. If present, it must be at the end of the token ("ab,", "cd!", and "." are valid, but "a!b" and "c.," are not valid).
Examples of valid words include "a-b.", "afad", "ba-c", "a!", and
"!".
Given a string sentence, return _thenumber of valid words in _sentence.
Input: sentence ="!this 1-s b8d!"Output: 0Explanation: There are no valid words in the sentence."!this"is invalid because it starts with a punctuation mark."1-s" and "b8d" are invalid because they contain digits.
Input: sentence ="_alice_ _and_ _bob_ _are_ _playing_ stone-game10"Output: 5Explanation: The valid words in the sentence are "alice","and","bob","are", and "playing"."stone-game10"is invalid because it contains digits.
We need to check each token in the sentence for the three validity rules. This can be done by iterating through each character and checking for digits, hyphen placement, and punctuation placement.
classSolution {
public:int countValidWords(string sentence) {
istringstream iss(sentence);
string token;
int ans =0;
while (iss >> token) {
bool valid = true;
int hyphen =0, punct =0, n = token.size();
for (int i =0; i < n; ++i) {
char c = token[i];
if (isdigit(c)) { valid = false; break; }
if (c =='-') {
hyphen++;
if (hyphen >1|| i ==0|| i == n-1||!islower(token[i-1]) ||!islower(token[i+1])) { valid = false; break; }
}
if (c =='!'|| c =='.'|| c ==',') {
if (i != n-1||++punct >1) { valid = false; break; }
}
}
if (valid) ans++;
}
return ans;
}
};
classSolution {
funcountValidWords(sentence: String): Int {
val tokens = sentence.trim().split(Regex("\\s+")).filter { it.isNotEmpty() }
var ans = 0for (token in tokens) {
var valid = truevar hyphen = 0; var punct = 0; val n = token.length
for (i in0 until n) {
val c = token[i]
if (c.isDigit()) { valid = false; break }
if (c =='-') {
hyphen++if (hyphen > 1|| i ==0|| i == n-1|| !token[i-1].isLowerCase() || !token[i+1].isLowerCase()) { valid = false; break }
}
if (c =='!'|| c =='.'|| c ==',') {
if (i != n-1||++punct > 1) { valid = false; break }
}
}
if (valid) ans++ }
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution:
defcountValidWords(self, sentence: str) -> int:
defvalid(token: str) -> bool:
hyphen = punct =0 n = len(token)
for i, c in enumerate(token):
if c.isdigit(): returnFalseif c =='-':
hyphen +=1if hyphen >1or i ==0or i == n-1ornot token[i-1].islower() ornot token[i+1].islower():
returnFalseif c in'!.,':
if i != n-1or punct+1>1:
returnFalse punct +=1returnTruereturn sum(valid(token) for token in sentence.strip().split())
impl Solution {
pubfncount_valid_words(sentence: String) -> i32 {
fnvalid(token: &str) -> bool {
letmut hyphen =0;
letmut punct =0;
let n = token.len();
let chars: Vec<char>= token.chars().collect();
for (i, &c) in chars.iter().enumerate() {
if c.is_ascii_digit() { returnfalse; }
if c =='-' {
hyphen +=1;
if hyphen >1|| i ==0|| i == n-1||!chars[i-1].is_ascii_lowercase() ||!chars[i+1].is_ascii_lowercase() { returnfalse; }
}
if c =='!'|| c =='.'|| c ==',' {
if i != n-1|| punct+1>1 { returnfalse; }
punct +=1;
}
}
true }
sentence.split_whitespace().filter(|t| valid(t)).count() asi32 }
}