Skip to content

Commit 9b8e639

Browse files
authored
feat: add solutions to lc problem: No.1002 (doocs#948)
1 parent 017f330 commit 9b8e639

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

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

+30
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,34 @@ func min(a, b int) int {
155155
}
156156
```
157157

158+
### **TypeScript**
159+
160+
```ts
161+
function commonChars(words: string[]): string[] {
162+
const freq: number[] = new Array(26).fill(10000);
163+
for (const word of words) {
164+
const t: number[] = new Array(26).fill(0);
165+
for (const c of word.split('')) {
166+
++t[c.charCodeAt(0) - 'a'.charCodeAt(0)];
167+
}
168+
for (let i = 0; i < 26; ++i) {
169+
freq[i] = Math.min(freq[i], t[i]);
170+
}
171+
}
172+
const res: string[] = [];
173+
for (let i = 0; i < 26; ++i) {
174+
while (freq[i]-- > 0) {
175+
res.push(String.fromCharCode(i + 'a'.charCodeAt(0)));
176+
}
177+
}
178+
return res;
179+
}
180+
```
181+
182+
### **...**
183+
184+
```
185+
186+
```
187+
158188
<!-- tabs:end -->

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

+24
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,30 @@ func min(a, b int) int {
132132
}
133133
```
134134

135+
### **TypeScript**
136+
137+
```ts
138+
function commonChars(words: string[]): string[] {
139+
const freq: number[] = new Array(26).fill(10000);
140+
for (const word of words) {
141+
const t: number[] = new Array(26).fill(0);
142+
for (const c of word.split('')) {
143+
++t[c.charCodeAt(0) - 'a'.charCodeAt(0)];
144+
}
145+
for (let i = 0; i < 26; ++i) {
146+
freq[i] = Math.min(freq[i], t[i]);
147+
}
148+
}
149+
const res: string[] = [];
150+
for (let i = 0; i < 26; ++i) {
151+
while (freq[i]-- > 0) {
152+
res.push(String.fromCharCode(i + 'a'.charCodeAt(0)));
153+
}
154+
}
155+
return res;
156+
}
157+
```
158+
135159
### **...**
136160

137161
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function commonChars(words: string[]): string[] {
2+
const freq: number[] = new Array(26).fill(10000);
3+
for (const word of words) {
4+
const t: number[] = new Array(26).fill(0);
5+
for (const c of word.split('')) {
6+
++t[c.charCodeAt(0) - 'a'.charCodeAt(0)];
7+
}
8+
for (let i = 0; i < 26; ++i) {
9+
freq[i] = Math.min(freq[i], t[i]);
10+
}
11+
}
12+
const res: string[] = [];
13+
for (let i = 0; i < 26; ++i) {
14+
while (freq[i]-- > 0) {
15+
res.push(String.fromCharCode(i + 'a'.charCodeAt(0)));
16+
}
17+
}
18+
return res;
19+
}

0 commit comments

Comments
 (0)