-
Notifications
You must be signed in to change notification settings - Fork 0
/
number-of-valid-words-in-a-sentence.py
43 lines (42 loc) · 1.56 KB
/
number-of-valid-words-in-a-sentence.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# https://leetcode.com/problems/number-of-valid-words-in-a-sentence/
class Solution:
def countValidWords(self, sentence: str) -> int:
word = ''
isDigit = False
hyphens = []
punctuationsMarks = []
result = 0
for index, letter in enumerate(sentence + ' '):
if letter != ' ':
if not word:
beginWordIndex = index
isDigit, hyphens, punctuationsMarks = False, [], []
word += letter
if letter.isdigit():
isDigit = True
elif letter == '-':
hyphens.append(index - beginWordIndex) # noqa
elif letter in ('!', '.', ','):
punctuationsMarks.append(letter)
continue
if not word:
continue
if isDigit or len(hyphens) > 1 or len(punctuationsMarks) > 1:
word = ''
continue
if hyphens:
if len(word) < 3 or '-' in (word[0], word[-1]):
word = ''
continue
i = hyphens[0]
if not word[i - 1].isalpha() or not word[i + 1].isalpha():
word = ''
continue
if punctuationsMarks:
mark = punctuationsMarks[0]
if word[-1] != mark:
word = ''
continue
result += 1
word, isDigit, hyphens, punctuationsMarks = '', False, [], []
return result