2047. 句子中的有效单词数
为保证权益,题目请参考 2047. 句子中的有效单词数(From LeetCode).
解决方案1
Python
python
# 2047. 句子中的有效单词数
# https://leetcode-cn.com/problems/number-of-valid-words-in-a-sentence/
################################################################################
from functools import reduce
import re
class Solution:
def countValidWords(self, sentence: str) -> int:
def isOk(word: str) -> bool:
ans = re.match(
"^(([a-z]*(([a-z]\-[a-z])|[a-z])[a-z]*[\!\.,]?)|[\!\.,])$", word
)
return ans is not None
t = reduce(lambda x, y: x + (1 if isOk(y) else 0), sentence.split(" "), 0)
return t
################################################################################
if __name__ == "__main__":
solution = Solution()
print(solution.countValidWords("alice and bob are playing stone-game10"))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28