classSolution { public: intcountValidWords(string sentence){ int n = sentence.length(); int l = 0, r = 0; int ret = 0; string_view slice(sentence); while (true) { while (l < n && sentence[l] == ' ') { l++; } if (l >= n) { break; } r = l + 1; while (r < n && sentence[r] != ' ') { r++; } if (isValid(slice.substr(l, r - l))) { // 判断根据空格分解出来的 token 是否有效 ret++; } l = r + 1; } return ret; }
boolisValid(const string_view &word){ int n = word.length(); bool has_hyphens = false; for (int i = 0; i < n; i++) { if (word[i] >= '0' && word[i] <= '9') { returnfalse; } elseif (word[i] == '-') { if (has_hyphens == true || i == 0 || i == n - 1 || !islower(word[i - 1]) || !islower(word[i + 1])) { returnfalse; } has_hyphens = true; } elseif (word[i] == '!' || word[i] == '.' || word[i] == ',') { if (i != n - 1) { returnfalse; } } } returntrue; } };
publicclassSolution { publicintCountValidWords(string sentence) { int n = sentence.Length; int l = 0, r = 0; int ret = 0; while (true) { while (l < n && sentence[l] == ' ') { l++; } if (l >= n) { break; } r = l + 1; while (r < n && sentence[r] != ' ') { r++; } if (isValid(sentence.Substring(l, r - l))) { // 判断根据空格分解出来的 token 是否有效 ret++; } l = r + 1; } return ret; }
publicboolisValid(string word) { int n = word.Length; bool hasHyphens = false; for (int i = 0; i < n; i++) { if (char.IsDigit(word[i])) { returnfalse; } elseif (word[i] == '-') { if (hasHyphens == true || i == 0 || i == n - 1 || !char.IsLetter(word[i - 1]) || !char.IsLetter(word[i + 1])) { returnfalse; } hasHyphens = true; } elseif (word[i] == '!' || word[i] == '.' || word[i] == ',') { if (i != n - 1) { returnfalse; } } } returntrue; } }
boolisValid(char *word, int n) { int has_hyphens = 0; for (int i = 0; i < n; i++) { if (word[i] >= '0' && word[i] <= '9') { returnfalse; } elseif (word[i] == '-') { if (has_hyphens == 1 || i == 0 || i == n - 1 || !islower(word[i - 1]) || !islower(word[i + 1])) { returnfalse; } has_hyphens = 1; } elseif (word[i] == '!' || word[i] == '.' || word[i] == ',') { if (i != n - 1) { returnfalse; } } } returntrue; }
intcountValidWords(char * sentence){ int n = strlen(sentence); int l = 0, r = 0; int ret = 0; while (true) { while (l < n && sentence[l] == ' ') { l++; } if (l >= n) { break; } r = l + 1; while (r < n && sentence[r] != ' ') { r++; } if (isValid(sentence + l, r - l)) { // 判断根据空格分解出来的 token 是否有效 ret++; } l = r + 1; } return ret; }
var countValidWords = function(sentence) { const n = sentence.length; let l = 0, r = 0; let ret = 0; while (true) { while (l < n && sentence[l] === ' ') { l++; } if (l >= n) { break; } r = l + 1; while (r < n && sentence[r] != ' ') { r++; } if (isValid(sentence.slice(l, r))) { // 判断根据空格分解出来的 token 是否有效 ret++; } l = r + 1; } return ret; };
constisValid = (word) => { const n = word.length; let hasHyphens = false; for (let i = 0; i < n; i++) { if (word[i] >= '0' && word[i] <= '9') { returnfalse; } elseif (word[i] === '-') { if (hasHyphens === true || i === 0 || i === n - 1 || !isLetter(word[i - 1]) || !isLetter(word[i + 1])) { returnfalse; } hasHyphens = true; } elseif (word[i] === '!' || word[i] === '.' || word[i] === ',') { if (i !== n - 1) { returnfalse; } } } returntrue; }