File tree Expand file tree Collapse file tree 3 files changed +119
-0
lines changed
solution/2000-2099/2047.Number of Valid Words in a Sentence Expand file tree Collapse file tree 3 files changed +119
-0
lines changed Original file line number Diff line number Diff line change 85
85
86
86
```
87
87
88
+ ### ** TypeScript**
89
+
90
+ ``` ts
91
+ function countValidWords(sentence : string ): number {
92
+ let words = sentence .trim ().split (/ \s + / );
93
+ let ans = 0 ;
94
+ for (let word of words ) {
95
+ if (isValied (word )) {
96
+ ans ++ ;
97
+ }
98
+ }
99
+ return ans ;
100
+ };
101
+
102
+ function isValied(str : string ): boolean {
103
+ let n = str .length ;
104
+ let hasLine = false ;
105
+ for (let i = 0 ; i < n ; i ++ ) {
106
+ const char = str .charAt (i );
107
+ if ((/ ^ [0-9 ] $ / ).test (char )) {
108
+ return false ;
109
+ }
110
+ if (char == ' -' ) {
111
+ if (hasLine ) return false ;
112
+ else {
113
+ hasLine = true ;
114
+ }
115
+ let pre = str .charAt (i - 1 ),
116
+ post = str .charAt (i + 1 );
117
+ if (! (/ ^ [a-z ] $ / g ).test (pre ) || ! (/ ^ [a-z ] $ / g ).test (post )) {
118
+ return false ;
119
+ }
120
+ }
121
+ if ((/ ^ [\!\.\, \s ] $ / ).test (char ) && i != n - 1 ) {
122
+ return false ;
123
+ }
124
+ }
125
+ return true ;
126
+ }
127
+ ```
128
+
88
129
### ** ...**
89
130
90
131
```
Original file line number Diff line number Diff line change 79
79
80
80
```
81
81
82
+ ### ** TypeScript**
83
+
84
+ ``` ts
85
+ function countValidWords(sentence : string ): number {
86
+ let words = sentence .trim ().split (/ \s + / );
87
+ let ans = 0 ;
88
+ for (let word of words ) {
89
+ if (isValied (word )) {
90
+ ans ++ ;
91
+ }
92
+ }
93
+ return ans ;
94
+ };
95
+
96
+ function isValied(str : string ): boolean {
97
+ let n = str .length ;
98
+ let hasLine = false ;
99
+ for (let i = 0 ; i < n ; i ++ ) {
100
+ const char = str .charAt (i );
101
+ if ((/ ^ [0-9 ] $ / ).test (char )) {
102
+ return false ;
103
+ }
104
+ if (char == ' -' ) {
105
+ if (hasLine ) return false ;
106
+ else {
107
+ hasLine = true ;
108
+ }
109
+ let pre = str .charAt (i - 1 ),
110
+ post = str .charAt (i + 1 );
111
+ if (! (/ ^ [a-z ] $ / g ).test (pre ) || ! (/ ^ [a-z ] $ / g ).test (post )) {
112
+ return false ;
113
+ }
114
+ }
115
+ if ((/ ^ [\!\.\, \s ] $ / ).test (char ) && i != n - 1 ) {
116
+ return false ;
117
+ }
118
+ }
119
+ return true ;
120
+ }
121
+ ```
122
+
82
123
### ** ...**
83
124
84
125
```
Original file line number Diff line number Diff line change
1
+
2
+ function countValidWords ( sentence : string ) : number {
3
+ let words = sentence . trim ( ) . split ( / \s + / ) ;
4
+ let ans = 0 ;
5
+ for ( let word of words ) {
6
+ if ( isValied ( word ) ) {
7
+ ans ++ ;
8
+ }
9
+ }
10
+ return ans ;
11
+ } ;
12
+
13
+ function isValied ( str : string ) : boolean {
14
+ let n = str . length ;
15
+ let hasLine = false ;
16
+ for ( let i = 0 ; i < n ; i ++ ) {
17
+ const char = str . charAt ( i ) ;
18
+ if ( ( / ^ [ 0 - 9 ] $ / ) . test ( char ) ) {
19
+ return false ;
20
+ }
21
+ if ( char == '-' ) {
22
+ if ( hasLine ) return false ;
23
+ else {
24
+ hasLine = true ;
25
+ }
26
+ let pre = str . charAt ( i - 1 ) ,
27
+ post = str . charAt ( i + 1 ) ;
28
+ if ( ! ( / ^ [ a - z ] $ / g) . test ( pre ) || ! ( / ^ [ a - z ] $ / g) . test ( post ) ) {
29
+ return false ;
30
+ }
31
+ }
32
+ if ( ( / ^ [ \! \. \, \s ] $ / ) . test ( char ) && i != n - 1 ) {
33
+ return false ;
34
+ }
35
+ }
36
+ return true ;
37
+ }
You can’t perform that action at this time.
0 commit comments