File tree Expand file tree Collapse file tree 4 files changed +136
-2
lines changed
solution/2000-2099/2047.Number of Valid Words in a Sentence Expand file tree Collapse file tree 4 files changed +136
-2
lines changed Original file line number Diff line number Diff line change 7474<!-- 这里可写当前语言的特殊实现逻辑 -->
7575
7676``` 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())
7891```
7992
8093### ** Java**
8194
8295<!-- 这里可写当前语言的特殊实现逻辑 -->
8396
8497``` 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+ }
85108
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+ }
86130```
87131
88132### ** TypeScript**
Original file line number Diff line number Diff line change 7070### ** Python3**
7171
7272``` 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())
7487```
7588
7689### ** Java**
7790
7891``` 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+ }
79102
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+ }
80124```
81125
82126### ** 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