-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.ts
36 lines (35 loc) · 914 Bytes
/
Solution.ts
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
function countValidWords(sentence: string): number {
let words = sentence.trim().split(/\s+/);
let ans = 0;
for (let word of words) {
if (isValied(word)) {
ans++;
}
}
return ans;
}
function isValied(str: string): boolean {
let n = str.length;
let hasLine = false;
for (let i = 0; i < n; i++) {
const char = str.charAt(i);
if (/^[0-9]$/.test(char)) {
return false;
}
if (char == '-') {
if (hasLine) return false;
else {
hasLine = true;
}
let pre = str.charAt(i - 1),
post = str.charAt(i + 1);
if (!/^[a-z]$/g.test(pre) || !/^[a-z]$/g.test(post)) {
return false;
}
}
if (/^[\!\.\,\s]$/.test(char) && i != n - 1) {
return false;
}
}
return true;
}