需要根据要求
class Solution:
def countValidWords(self, sentence: str) -> int:
# 先定义了一个确定的函数
def valid(s: str) -> bool:
hasHyphens = False
for i, ch in enumerate(s):
# isdigit() 判断是否为数字
if ch.isdigit() or ch in "!.," and i < len(s) - 1: # 字符且不是最后
return False
if ch == '-': # 连字符 hasHypens forse 表明只出现一次 判断是否小写如果存在,连字符两侧应当都存在小写字母
if hasHyphens or i == 0 or i == len(s) - 1 or not s[i - 1].islower() or not s[i + 1].islower():
return False
hasHyphens = True
return True
s_all = sentence.split()
num = 0
for s_i in s_all:
if valid(s_i):
num += 1
return num