【算法训练】LeetCode#2047 句子中的有效单词数

一、描述

句子中的有效单词数

句子仅由小写字母('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."

二、思路

  • 通过splt分词后依照规则逐字符判断

三、解题

class Solution:
    def countValidWords(self, sentence: str) -> int:
        # 字母list
        letter_list = [chr(i) for i in range(97, 123)]
        # 符号list
        symbol_list = ['!','.',',']
        # 按空格分割
        splt_list = sentence.split()
        # 记录个数
        result = 0
        for word in splt_list:
            word_len = len(word)
            num = 0
            for loc in range(word_len):
                # 如果字符不合法
                if word[loc] not in symbol_list and word[loc] not in letter_list and word[loc] != '-':
                    break
                # 如果当前为符号
                if word[loc] in symbol_list:
                    if loc != word_len-1:
                        break
                # 如果当前为'-'
                if word[loc] == '-':
                    num += 1
                    # 如果在头或者尾部
                    if loc == 0 or loc == word_len-1:
                        break
                    #  如果'-'个数大于1
                    if num == 2:
                        break
                    # 如果前后不为字母
                    if word[loc-1] not in letter_list or word[loc+1] not in letter_list:
                        break
            else:
                result += 1
        return result

posted @ 2022-01-27 16:51  小拳头呀  阅读(31)  评论(0编辑  收藏  举报