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 292ced6

Browse files
authored
refactor: update ts solution to lc problem: No.1002 (doocs#3044)
1 parent 1dc1959 commit 292ced6

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

‎solution/1000-1099/1002.Find Common Characters/README.md‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,23 +164,24 @@ func commonChars(words []string) (ans []string) {
164164

165165
```ts
166166
function commonChars(words: string[]): string[] {
167-
const freq: number[] = new Array(26).fill(10000);
167+
const freq: number[] = Array(26).fill(Number.POSITIVE_INFINITY);
168+
const aCode = 'a'.charCodeAt(0);
168169
for (const word of words) {
169-
const t: number[] = newArray(26).fill(0);
170-
for (const c of word.split('')) {
171-
++t[c.charCodeAt(0) - 'a'.charCodeAt(0)];
170+
const t: number[] = Array(26).fill(0);
171+
for (const c of word) {
172+
++t[c.charCodeAt(0) - aCode];
172173
}
173174
for (let i = 0; i < 26; ++i) {
174175
freq[i] = Math.min(freq[i], t[i]);
175176
}
176177
}
177-
const res: string[] = [];
178+
const ans: string[] = [];
178179
for (let i = 0; i < 26; ++i) {
179-
while (freq[i]-->0) {
180-
res.push(String.fromCharCode(i + 'a'.charCodeAt(0)));
180+
if (freq[i]) {
181+
ans.push(...String.fromCharCode(i + aCode).repeat(freq[i]));
181182
}
182183
}
183-
return res;
184+
return ans;
184185
}
185186
```
186187

‎solution/1000-1099/1002.Find Common Characters/README_EN.md‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,23 +151,24 @@ func commonChars(words []string) (ans []string) {
151151

152152
```ts
153153
function commonChars(words: string[]): string[] {
154-
const freq: number[] = new Array(26).fill(10000);
154+
const freq: number[] = Array(26).fill(Number.POSITIVE_INFINITY);
155+
const aCode = 'a'.charCodeAt(0);
155156
for (const word of words) {
156-
const t: number[] = newArray(26).fill(0);
157-
for (const c of word.split('')) {
158-
++t[c.charCodeAt(0) - 'a'.charCodeAt(0)];
157+
const t: number[] = Array(26).fill(0);
158+
for (const c of word) {
159+
++t[c.charCodeAt(0) - aCode];
159160
}
160161
for (let i = 0; i < 26; ++i) {
161162
freq[i] = Math.min(freq[i], t[i]);
162163
}
163164
}
164-
const res: string[] = [];
165+
const ans: string[] = [];
165166
for (let i = 0; i < 26; ++i) {
166-
while (freq[i]-->0) {
167-
res.push(String.fromCharCode(i + 'a'.charCodeAt(0)));
167+
if (freq[i]) {
168+
ans.push(...String.fromCharCode(i + aCode).repeat(freq[i]));
168169
}
169170
}
170-
return res;
171+
return ans;
171172
}
172173
```
173174

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
function commonChars(words: string[]): string[] {
2-
const freq: number[] = new Array(26).fill(10000);
2+
const freq: number[] = Array(26).fill(Number.POSITIVE_INFINITY);
3+
const aCode = 'a'.charCodeAt(0);
34
for (const word of words) {
4-
const t: number[] = newArray(26).fill(0);
5-
for (const c of word.split('')) {
6-
++t[c.charCodeAt(0) - 'a'.charCodeAt(0)];
5+
const t: number[] = Array(26).fill(0);
6+
for (const c of word) {
7+
++t[c.charCodeAt(0) - aCode];
78
}
89
for (let i = 0; i < 26; ++i) {
910
freq[i] = Math.min(freq[i], t[i]);
1011
}
1112
}
12-
const res: string[] = [];
13+
const ans: string[] = [];
1314
for (let i = 0; i < 26; ++i) {
14-
while (freq[i]-->0) {
15-
res.push(String.fromCharCode(i + 'a'.charCodeAt(0)));
15+
if (freq[i]) {
16+
ans.push(...String.fromCharCode(i + aCode).repeat(freq[i]));
1617
}
1718
}
18-
return res;
19+
return ans;
1920
}

0 commit comments

Comments
(0)

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