Skip to content

Commit e2a9a60

Browse files
committed
feat: add ts solution to lc problem: No.0726
1 parent cf59818 commit e2a9a60

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed

solution/0700-0799/0726.Number of Atoms/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,74 @@ class Solution {
155155

156156
```
157157

158+
#### TypeScript
159+
160+
```ts
161+
function countOfAtoms(formula: string): string {
162+
const getCount = (formula: string, factor = 1) => {
163+
const n = formula.length;
164+
const cnt: Record<string, number> = {};
165+
const s: string[] = [];
166+
let [atom, c] = ['', 0];
167+
168+
for (let i = 0; i <= n; i++) {
169+
if (formula[i] === '(') {
170+
const stk: string[] = ['('];
171+
let j = i;
172+
while (stk.length) {
173+
j++;
174+
if (formula[j] === '(') stk.push('(');
175+
else if (formula[j] === ')') stk.pop();
176+
}
177+
178+
const molecule = formula.slice(i + 1, j);
179+
const nextFactor: string[] = [];
180+
181+
while (isDigit(formula[++j])) {
182+
nextFactor.push(formula[j]);
183+
}
184+
185+
const nextC = getCount(molecule, +nextFactor.join('') || 1);
186+
for (const [atom, c] of Object.entries(nextC)) {
187+
cnt[atom] = (cnt[atom] ?? 0) + c * factor;
188+
}
189+
190+
i = j - 1;
191+
continue;
192+
}
193+
194+
if (s.length && (!formula[i] || isUpper(formula[i]))) {
195+
[atom, c] = getAtom(s);
196+
197+
c *= factor;
198+
cnt[atom] = (cnt[atom] ?? 0) + c;
199+
s.length = 0;
200+
}
201+
202+
s.push(formula[i]);
203+
}
204+
205+
return cnt;
206+
};
207+
208+
return Object.entries(getCount(formula))
209+
.sort(([a], [b]) => a.localeCompare(b))
210+
.map(([a, b]) => (b > 1 ? a + b : a))
211+
.join('');
212+
}
213+
214+
const regex = {
215+
atom: /(\D+)(\d+)?/,
216+
isUpper: /[A-Z]+/,
217+
};
218+
const getAtom = (s: string[]): [string, number] => {
219+
const [_, atom, c] = regex.atom.exec(s.join(''))!;
220+
return [atom, c ? +c : 1];
221+
};
222+
const isDigit = (ch: string) => !Number.isNaN(Number.parseInt(ch));
223+
const isUpper = (ch: string) => regex.isUpper.test(ch);
224+
```
225+
158226
<!-- tabs:end -->
159227

160228
<!-- solution:end -->

solution/0700-0799/0726.Number of Atoms/README_EN.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,74 @@ class Solution {
155155

156156
```
157157

158+
#### TypeScript
159+
160+
```ts
161+
function countOfAtoms(formula: string): string {
162+
const getCount = (formula: string, factor = 1) => {
163+
const n = formula.length;
164+
const cnt: Record<string, number> = {};
165+
const s: string[] = [];
166+
let [atom, c] = ['', 0];
167+
168+
for (let i = 0; i <= n; i++) {
169+
if (formula[i] === '(') {
170+
const stk: string[] = ['('];
171+
let j = i;
172+
while (stk.length) {
173+
j++;
174+
if (formula[j] === '(') stk.push('(');
175+
else if (formula[j] === ')') stk.pop();
176+
}
177+
178+
const molecule = formula.slice(i + 1, j);
179+
const nextFactor: string[] = [];
180+
181+
while (isDigit(formula[++j])) {
182+
nextFactor.push(formula[j]);
183+
}
184+
185+
const nextC = getCount(molecule, +nextFactor.join('') || 1);
186+
for (const [atom, c] of Object.entries(nextC)) {
187+
cnt[atom] = (cnt[atom] ?? 0) + c * factor;
188+
}
189+
190+
i = j - 1;
191+
continue;
192+
}
193+
194+
if (s.length && (!formula[i] || isUpper(formula[i]))) {
195+
[atom, c] = getAtom(s);
196+
197+
c *= factor;
198+
cnt[atom] = (cnt[atom] ?? 0) + c;
199+
s.length = 0;
200+
}
201+
202+
s.push(formula[i]);
203+
}
204+
205+
return cnt;
206+
};
207+
208+
return Object.entries(getCount(formula))
209+
.sort(([a], [b]) => a.localeCompare(b))
210+
.map(([a, b]) => (b > 1 ? a + b : a))
211+
.join('');
212+
}
213+
214+
const regex = {
215+
atom: /(\D+)(\d+)?/,
216+
isUpper: /[A-Z]+/,
217+
};
218+
const getAtom = (s: string[]): [string, number] => {
219+
const [_, atom, c] = regex.atom.exec(s.join(''))!;
220+
return [atom, c ? +c : 1];
221+
};
222+
const isDigit = (ch: string) => !Number.isNaN(Number.parseInt(ch));
223+
const isUpper = (ch: string) => regex.isUpper.test(ch);
224+
```
225+
158226
<!-- tabs:end -->
159227

160228
<!-- solution:end -->
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
function countOfAtoms(formula: string): string {
2+
const getCount = (formula: string, factor = 1) => {
3+
const n = formula.length;
4+
const cnt: Record<string, number> = {};
5+
const s: string[] = [];
6+
let [atom, c] = ['', 0];
7+
8+
for (let i = 0; i <= n; i++) {
9+
if (formula[i] === '(') {
10+
const stk: string[] = ['('];
11+
let j = i;
12+
while (stk.length) {
13+
j++;
14+
if (formula[j] === '(') stk.push('(');
15+
else if (formula[j] === ')') stk.pop();
16+
}
17+
18+
const molecule = formula.slice(i + 1, j);
19+
const nextFactor: string[] = [];
20+
21+
while (isDigit(formula[++j])) {
22+
nextFactor.push(formula[j]);
23+
}
24+
25+
const nextC = getCount(molecule, +nextFactor.join('') || 1);
26+
for (const [atom, c] of Object.entries(nextC)) {
27+
cnt[atom] = (cnt[atom] ?? 0) + c * factor;
28+
}
29+
30+
i = j - 1;
31+
continue;
32+
}
33+
34+
if (s.length && (!formula[i] || isUpper(formula[i]))) {
35+
[atom, c] = getAtom(s);
36+
37+
c *= factor;
38+
cnt[atom] = (cnt[atom] ?? 0) + c;
39+
s.length = 0;
40+
}
41+
42+
s.push(formula[i]);
43+
}
44+
45+
return cnt;
46+
};
47+
48+
return Object.entries(getCount(formula))
49+
.sort(([a], [b]) => a.localeCompare(b))
50+
.map(([a, b]) => (b > 1 ? a + b : a))
51+
.join('');
52+
}
53+
54+
const regex = {
55+
atom: /(\D+)(\d+)?/,
56+
isUpper: /[A-Z]+/,
57+
};
58+
const getAtom = (s: string[]): [string, number] => {
59+
const [_, atom, c] = regex.atom.exec(s.join(''))!;
60+
return [atom, c ? +c : 1];
61+
};
62+
const isDigit = (ch: string) => !Number.isNaN(Number.parseInt(ch));
63+
const isUpper = (ch: string) => regex.isUpper.test(ch);

0 commit comments

Comments
 (0)