Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fa64fa3

Browse files
feat: add typescript solution to lc problem: No.2047 (doocs#594)
No.2047.Number of Valid Words in a Sentence
1 parent 08c51f5 commit fa64fa3

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

‎solution/2000-2099/2047.Number of Valid Words in a Sentence/README.md‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,47 @@
8585

8686
```
8787

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+
88129
### **...**
89130

90131
```

‎solution/2000-2099/2047.Number of Valid Words in a Sentence/README_EN.md‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,47 @@
7979

8080
```
8181

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+
82123
### **...**
83124

84125
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /