Open carloscn opened 1 year ago
static bool is_valid(const char *str, bool is_tail)
{
if (NULL == str) {
return false;
}
size_t len = strlen(str);
if (0 == len) {
return false;
}
size_t digital_count = 0;
size_t alpha_count = 0;
size_t punctuation_count = 0;
for (size_t i = 0; i < len; i ++) {
if (isalpha(str[i])) {
if (digital_count != 0 || punctuation_count != 0) {
return false;
}
alpha_count ++;
} else if (isdigit(str[i])) {
if (alpha_count != 0 || punctuation_count != 0) {
return false;
}
digital_count ++;
} else if (str[i] == '-') {
if (digital_count != 0 && alpha_count != 0) {
return false;
} else if (digital_count == 0 && alpha_count != 0) {
if (i != len - 1) {
alpha_count ++;
} else {
return false;
}
} else if (digital_count != 0 && alpha_count == 0) {
return false;
}
} else if (str[i] == '.' || str[i] == '!') {
if (i == len - 1 && is_tail == true) {
return true;
}
punctuation_count ++;
} else if (str[i] == ',') {
if (i == len - 1 && is_tail == false) {
return true;
}
punctuation_count ++;
} else {
return false;
}
}
return true;
}
static int32_t count_valid_words(const char *sentence, size_t *count)
{
int32_t ret = 0;
size_t len = 0;
UTILS_CHECK_PTR(sentence);
UTILS_CHECK_PTR(count);
UTILS_CHECK_LEN(len = strlen(sentence));
char temp[100] = {0};
int32_t i = 0;
int32_t j = -1;
*count = 0;
while (i < len + 1) {
if (sentence[i] == ' ' || sentence[i] == '\0') {
if (i - j != 1) {
memcpy(temp, sentence + j + 1, i - j - 1);
temp[i - j] = '\0';
if (is_valid(temp, sentence[i] == '\0')) {
(*count) ++;
}
}
j = i;
}
i ++;
}
finish:
return ret;
}
Description
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 the number of valid words in sentence.
Example 1:
Input: sentence = "cat and dog" Output: 3 Explanation: The valid words in the sentence are "cat", "and", and "dog".
Example 2:
Input: sentence = "!this 1-s b8d!" Output: 0 Explanation: 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.
Example 3:
Input: sentence = "alice and bob are playing stone-game10" Output: 5 Explanation: The valid words in the sentence are "alice", "and", "bob", "are", and "playing". "stone-game10" is invalid because it contains digits.
Constraints:
1 <= sentence.length <= 1000 sentence only contains lowercase English letters, digits, ' ', '-', '!', '.', and ','. There will be at least 1 token.