File tree 4 files changed +136
-2
lines changed
solution/2000-2099/2047.Number of Valid Words in a Sentence
4 files changed +136
-2
lines changed Original file line number Diff line number Diff line change 74
74
<!-- 这里可写当前语言的特殊实现逻辑 -->
75
75
76
76
``` python
77
-
77
+ class Solution :
78
+ def countValidWords (self , sentence : str ) -> int :
79
+ def check (token ):
80
+ hyphen = False
81
+ for i, c in enumerate (token):
82
+ if c.isdigit() or (c in ' !.,' and i < len (token) - 1 ):
83
+ return False
84
+ if c == ' -' :
85
+ if hyphen or i == 0 or i == len (token) - 1 or not token[i - 1 ].islower() or not token[i + 1 ].islower():
86
+ return False
87
+ hyphen = True
88
+ return True
89
+
90
+ return sum (check(token) for token in sentence.split())
78
91
```
79
92
80
93
### ** Java**
81
94
82
95
<!-- 这里可写当前语言的特殊实现逻辑 -->
83
96
84
97
``` java
98
+ class Solution {
99
+ public int countValidWords (String sentence ) {
100
+ int ans = 0 ;
101
+ for (String token : sentence. split(" " )) {
102
+ if (check(token)) {
103
+ ++ ans;
104
+ }
105
+ }
106
+ return ans;
107
+ }
85
108
109
+ private boolean check (String token ) {
110
+ int n = token. length();
111
+ if (n == 0 ) {
112
+ return false ;
113
+ }
114
+ boolean hyphen = false ;
115
+ for (int i = 0 ; i < n; ++ i) {
116
+ char c = token. charAt(i);
117
+ if (Character . isDigit(c) || (i < n - 1 && (c == ' !' || c == ' .' || c == ' ,' ))) {
118
+ return false ;
119
+ }
120
+ if (c == ' -' ) {
121
+ if (hyphen || i == 0 || i == n - 1 || ! Character . isLetter(token. charAt(i - 1 )) || ! Character . isLetter(token. charAt(i + 1 ))) {
122
+ return false ;
123
+ }
124
+ hyphen = true ;
125
+ }
126
+ }
127
+ return true ;
128
+ }
129
+ }
86
130
```
87
131
88
132
### ** TypeScript**
Original file line number Diff line number Diff line change 70
70
### ** Python3**
71
71
72
72
``` python
73
-
73
+ class Solution :
74
+ def countValidWords (self , sentence : str ) -> int :
75
+ def check (token ):
76
+ hyphen = False
77
+ for i, c in enumerate (token):
78
+ if c.isdigit() or (c in ' !.,' and i < len (token) - 1 ):
79
+ return False
80
+ if c == ' -' :
81
+ if hyphen or i == 0 or i == len (token) - 1 or not token[i - 1 ].islower() or not token[i + 1 ].islower():
82
+ return False
83
+ hyphen = True
84
+ return True
85
+
86
+ return sum (check(token) for token in sentence.split())
74
87
```
75
88
76
89
### ** Java**
77
90
78
91
``` java
92
+ class Solution {
93
+ public int countValidWords (String sentence ) {
94
+ int ans = 0 ;
95
+ for (String token : sentence. split(" " )) {
96
+ if (check(token)) {
97
+ ++ ans;
98
+ }
99
+ }
100
+ return ans;
101
+ }
79
102
103
+ private boolean check (String token ) {
104
+ int n = token. length();
105
+ if (n == 0 ) {
106
+ return false ;
107
+ }
108
+ boolean hyphen = false ;
109
+ for (int i = 0 ; i < n; ++ i) {
110
+ char c = token. charAt(i);
111
+ if (Character . isDigit(c) || (i < n - 1 && (c == ' !' || c == ' .' || c == ' ,' ))) {
112
+ return false ;
113
+ }
114
+ if (c == ' -' ) {
115
+ if (hyphen || i == 0 || i == n - 1 || ! Character . isLetter(token. charAt(i - 1 )) || ! Character . isLetter(token. charAt(i + 1 ))) {
116
+ return false ;
117
+ }
118
+ hyphen = true ;
119
+ }
120
+ }
121
+ return true ;
122
+ }
123
+ }
80
124
```
81
125
82
126
### ** TypeScript**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int countValidWords (String sentence ) {
3
+ int ans = 0 ;
4
+ for (String token : sentence .split (" " )) {
5
+ if (check (token )) {
6
+ ++ans ;
7
+ }
8
+ }
9
+ return ans ;
10
+ }
11
+
12
+ private boolean check (String token ) {
13
+ int n = token .length ();
14
+ if (n == 0 ) {
15
+ return false ;
16
+ }
17
+ boolean hyphen = false ;
18
+ for (int i = 0 ; i < n ; ++i ) {
19
+ char c = token .charAt (i );
20
+ if (Character .isDigit (c ) || (i < n - 1 && (c == '!' || c == '.' || c == ',' ))) {
21
+ return false ;
22
+ }
23
+ if (c == '-' ) {
24
+ if (hyphen || i == 0 || i == n - 1 || !Character .isLetter (token .charAt (i - 1 )) || !Character .isLetter (token .charAt (i + 1 ))) {
25
+ return false ;
26
+ }
27
+ hyphen = true ;
28
+ }
29
+ }
30
+ return true ;
31
+ }
32
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def countValidWords (self , sentence : str ) -> int :
3
+ def check (token ):
4
+ hyphen = False
5
+ for i , c in enumerate (token ):
6
+ if c .isdigit () or (c in '!.,' and i < len (token ) - 1 ):
7
+ return False
8
+ if c == '-' :
9
+ if hyphen or i == 0 or i == len (token ) - 1 or not token [i - 1 ].islower () or not token [i + 1 ].islower ():
10
+ return False
11
+ hyphen = True
12
+ return True
13
+
14
+ return sum (check (token ) for token in sentence .split ())
You can’t perform that action at this time.
0 commit comments