Skip to content

Commit 632966a

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

File tree

3 files changed

+202
-0
lines changed

3 files changed

+202
-0
lines changed

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,75 @@ 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+
console.log(s.join(''));
196+
[atom, c] = getAtom(s);
197+
198+
c *= factor;
199+
cnt[atom] = (cnt[atom] ?? 0) + c;
200+
s.length = 0;
201+
}
202+
203+
s.push(formula[i]);
204+
}
205+
206+
return cnt;
207+
};
208+
209+
return Object.entries(getCount(formula))
210+
.sort(([a], [b]) => a.localeCompare(b))
211+
.map(([a, b]) => (b > 1 ? a + b : a))
212+
.join('');
213+
}
214+
215+
const regex = {
216+
atom: /(\D+)(\d+)?/,
217+
isUpper: /[A-Z]+/,
218+
};
219+
const getAtom = (s: string[]): [string, number] => {
220+
const [_, atom, c] = regex.atom.exec(s.join(''))!;
221+
return [atom, c ? +c : 1];
222+
};
223+
const isDigit = (ch: string) => !Number.isNaN(Number.parseInt(ch));
224+
const isUpper = (ch: string) => regex.isUpper.test(ch);
225+
```
226+
158227
<!-- tabs:end -->
159228

160229
<!-- solution:end -->

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,75 @@ 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+
console.log(s.join(''));
196+
[atom, c] = getAtom(s);
197+
198+
c *= factor;
199+
cnt[atom] = (cnt[atom] ?? 0) + c;
200+
s.length = 0;
201+
}
202+
203+
s.push(formula[i]);
204+
}
205+
206+
return cnt;
207+
};
208+
209+
return Object.entries(getCount(formula))
210+
.sort(([a], [b]) => a.localeCompare(b))
211+
.map(([a, b]) => (b > 1 ? a + b : a))
212+
.join('');
213+
}
214+
215+
const regex = {
216+
atom: /(\D+)(\d+)?/,
217+
isUpper: /[A-Z]+/,
218+
};
219+
const getAtom = (s: string[]): [string, number] => {
220+
const [_, atom, c] = regex.atom.exec(s.join(''))!;
221+
return [atom, c ? +c : 1];
222+
};
223+
const isDigit = (ch: string) => !Number.isNaN(Number.parseInt(ch));
224+
const isUpper = (ch: string) => regex.isUpper.test(ch);
225+
```
226+
158227
<!-- tabs:end -->
159228

160229
<!-- solution:end -->
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
console.log(s.join(''));
36+
[atom, c] = getAtom(s);
37+
38+
c *= factor;
39+
cnt[atom] = (cnt[atom] ?? 0) + c;
40+
s.length = 0;
41+
}
42+
43+
s.push(formula[i]);
44+
}
45+
46+
return cnt;
47+
};
48+
49+
return Object.entries(getCount(formula))
50+
.sort(([a], [b]) => a.localeCompare(b))
51+
.map(([a, b]) => (b > 1 ? a + b : a))
52+
.join('');
53+
}
54+
55+
const regex = {
56+
atom: /(\D+)(\d+)?/,
57+
isUpper: /[A-Z]+/,
58+
};
59+
const getAtom = (s: string[]): [string, number] => {
60+
const [_, atom, c] = regex.atom.exec(s.join(''))!;
61+
return [atom, c ? +c : 1];
62+
};
63+
const isDigit = (ch: string) => !Number.isNaN(Number.parseInt(ch));
64+
const isUpper = (ch: string) => regex.isUpper.test(ch);

0 commit comments

Comments
 (0)