加载中...
2047-句子中的有效单词数(Number of Valid Words in a Sentence)
发表于:2021-12-03 | 分类: 简单
字数统计: 1.2k | 阅读时长: 6分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/number-of-valid-words-in-a-sentence

英文原文

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.

Example 4:

Input: sentence = "he bought 2 pencils, 3 erasers, and 1  pencil-sharpener."
Output: 6
Explanation: The valid words in the sentence are "he", "bought", "pencils,", "erasers,", "and", and "pencil-sharpener.".

 

Constraints:

  • 1 <= sentence.length <= 1000
  • sentence only contains lowercase English letters, digits, ' ', '-', '!', '.', and ','.
  • There will be at least 1 token.

中文题目

句子仅由小写字母('a''z')、数字('0''9')、连字符('-')、标点符号('!''.'',')以及空格(' ')组成。每个句子可以根据空格分解成 一个或者多个 token ,这些 token 之间由一个或者多个空格 ' ' 分隔。

如果一个 token 同时满足下述条件,则认为这个 token 是一个有效单词:

  • 仅由小写字母、连字符和/或标点(不含数字)。
  • 至多一个 连字符 '-' 。如果存在,连字符两侧应当都存在小写字母("a-b" 是一个有效单词,但 "-ab""ab-" 不是有效单词)。
  • 至多一个 标点符号。如果存在,标点符号应当位于 token 的 末尾

这里给出几个有效单词的例子:"a-b.""afad""ba-c""a!""!"

给你一个字符串 sentence ,请你找出并返回 sentence 有效单词的数目

 

示例 1:

输入:sentence = "cat and  dog"
输出:3
解释:句子中的有效单词是 "cat"、"and" 和 "dog"

示例 2:

输入:sentence = "!this  1-s b8d!"
输出:0
解释:句子中没有有效单词
"!this" 不是有效单词,因为它以一个标点开头
"1-s" 和 "b8d" 也不是有效单词,因为它们都包含数字

示例 3:

输入:sentence = "alice and  bob are playing stone-game10"
输出:5
解释:句子中的有效单词是 "alice"、"and"、"bob"、"are" 和 "playing"
"stone-game10" 不是有效单词,因为它含有数字

示例 4:

输入:sentence = "he bought 2 pencils, 3 erasers, and 1  pencil-sharpener."
输出:6
解释:句子中的有效单词是 "he"、"bought"、"pencils,"、"erasers,"、"and" 和 "pencil-sharpener."

 

提示:

  • 1 <= sentence.length <= 1000
  • sentence 由小写英文字母、数字(0-9)、以及字符(' ''-''!''.'',')组成
  • 句子中至少有 1 个 token

通过代码

高赞题解

class Solution {
public:
    //判断函数
    bool jg(string s){
        int k=0;
        if(s.size()==0){return false;}        //可以很好的处理多个空格问题
        for(auto t:s){                       //先判断数字,这样条理更清晰
            if(isdigit(t)){return false;}//isdigit()函数判断字符串是否全为数字
        }
        for(int i=0;i<s.size();i++){
            if(s[i]==','||s[i]=='.'||s[i]=='!'){//再判断标点
                if(i!=s.size()-1){return false;}
            }
            if(s[i]=='-'){                 //最后判断-,到此已经不会再有数字或者别的符号了
                if(k++){return false;}  //判断是否只有有一个'-'
                if(i==0||i==s.size()-1||!isalpha(s[i+1])){return false;}
            }
        }
        return true;
    }
    int countValidWords(string sentence) {
        string t="";                    //用于存放每个小串
        int sum=0;
        for(int i=0;i<sentence.size();i++){//简单实现字符串分割
            if(sentence[i]==' '){
                if(jg(t)){sum++;}
                t="";  //不要忘记每次遇到空格t都要清空
                continue;
            }
            t+=sentence[i];
        }
        if(jg(t)){sum++;}                //最后一个小串的后面可能没有空格,要单独判断
        return sum;
    }
};

统计信息

通过次数 提交次数 AC比率
4785 16809 28.5%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
2045-到达目的地的第二短时间(Second Minimum Time to Reach Destination)
下一篇:
2048-下一个更大的数值平衡数(Next Greater Numerically Balanced Number)
本文目录
本文目录