Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions solution/0700-0799/0726.Number of Atoms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,146 @@ class Solution {

```

#### TypeScript

```ts
function countOfAtoms(formula: string): string {
const getCount = (formula: string, factor = 1) => {
const n = formula.length;
const cnt: Record<string, number> = {};
const s: string[] = [];
let [atom, c] = ['', 0];

for (let i = 0; i <= n; i++) {
if (formula[i] === '(') {
const stk: string[] = ['('];
let j = i;
while (stk.length) {
j++;
if (formula[j] === '(') stk.push('(');
else if (formula[j] === ')') stk.pop();
}

const molecule = formula.slice(i + 1, j);
const nextFactor: string[] = [];

while (isDigit(formula[++j])) {
nextFactor.push(formula[j]);
}

const nextC = getCount(molecule, +nextFactor.join('') || 1);
for (const [atom, c] of Object.entries(nextC)) {
cnt[atom] = (cnt[atom] ?? 0) + c * factor;
}

i = j - 1;
continue;
}

if (s.length && (!formula[i] || isUpper(formula[i]))) {
[atom, c] = getAtom(s);

c *= factor;
cnt[atom] = (cnt[atom] ?? 0) + c;
s.length = 0;
}

s.push(formula[i]);
}

return cnt;
};

return Object.entries(getCount(formula))
.sort(([a], [b]) => a.localeCompare(b))
.map(([a, b]) => (b > 1 ? a + b : a))
.join('');
}

const regex = {
atom: /(\D+)(\d+)?/,
isUpper: /[A-Z]+/,
};
const getAtom = (s: string[]): [string, number] => {
const [_, atom, c] = regex.atom.exec(s.join(''))!;
return [atom, c ? +c : 1];
};
const isDigit = (ch: string) => !Number.isNaN(Number.parseInt(ch));
const isUpper = (ch: string) => regex.isUpper.test(ch);
```

#### JavaScript

```js
/**
* @param {string} formula
* @return {string}
*/
var countOfAtoms = function (formula) {
const getCount = (formula, factor = 1) => {
const n = formula.length;
const cnt = {};
const s = [];
let [atom, c] = ['', 0];

for (let i = 0; i <= n; i++) {
if (formula[i] === '(') {
const stk = ['('];
let j = i;
while (stk.length) {
j++;
if (formula[j] === '(') stk.push('(');
else if (formula[j] === ')') stk.pop();
}

const molecule = formula.slice(i + 1, j);
const nextFactor = [];

while (isDigit(formula[++j])) {
nextFactor.push(formula[j]);
}

const nextC = getCount(molecule, +nextFactor.join('') || 1);
for (const [atom, c] of Object.entries(nextC)) {
cnt[atom] = (cnt[atom] ?? 0) + c * factor;
}

i = j - 1;
continue;
}

if (s.length && (!formula[i] || isUpper(formula[i]))) {
[atom, c] = getAtom(s);

c *= factor;
cnt[atom] = (cnt[atom] ?? 0) + c;
s.length = 0;
}

s.push(formula[i]);
}

return cnt;
};

return Object.entries(getCount(formula))
.sort(([a], [b]) => a.localeCompare(b))
.map(([a, b]) => (b > 1 ? a + b : a))
.join('');
};

const regex = {
atom: /(\D+)(\d+)?/,
isUpper: /[A-Z]+/,
};
const getAtom = s => {
const [_, atom, c] = regex.atom.exec(s.join(''));
return [atom, c ? +c : 1];
};
const isDigit = ch => !Number.isNaN(Number.parseInt(ch));
const isUpper = ch => regex.isUpper.test(ch);
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
140 changes: 140 additions & 0 deletions solution/0700-0799/0726.Number of Atoms/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,146 @@ class Solution {

```

#### TypeScript

```ts
function countOfAtoms(formula: string): string {
const getCount = (formula: string, factor = 1) => {
const n = formula.length;
const cnt: Record<string, number> = {};
const s: string[] = [];
let [atom, c] = ['', 0];

for (let i = 0; i <= n; i++) {
if (formula[i] === '(') {
const stk: string[] = ['('];
let j = i;
while (stk.length) {
j++;
if (formula[j] === '(') stk.push('(');
else if (formula[j] === ')') stk.pop();
}

const molecule = formula.slice(i + 1, j);
const nextFactor: string[] = [];

while (isDigit(formula[++j])) {
nextFactor.push(formula[j]);
}

const nextC = getCount(molecule, +nextFactor.join('') || 1);
for (const [atom, c] of Object.entries(nextC)) {
cnt[atom] = (cnt[atom] ?? 0) + c * factor;
}

i = j - 1;
continue;
}

if (s.length && (!formula[i] || isUpper(formula[i]))) {
[atom, c] = getAtom(s);

c *= factor;
cnt[atom] = (cnt[atom] ?? 0) + c;
s.length = 0;
}

s.push(formula[i]);
}

return cnt;
};

return Object.entries(getCount(formula))
.sort(([a], [b]) => a.localeCompare(b))
.map(([a, b]) => (b > 1 ? a + b : a))
.join('');
}

const regex = {
atom: /(\D+)(\d+)?/,
isUpper: /[A-Z]+/,
};
const getAtom = (s: string[]): [string, number] => {
const [_, atom, c] = regex.atom.exec(s.join(''))!;
return [atom, c ? +c : 1];
};
const isDigit = (ch: string) => !Number.isNaN(Number.parseInt(ch));
const isUpper = (ch: string) => regex.isUpper.test(ch);
```

#### JavaScript

```js
/**
* @param {string} formula
* @return {string}
*/
var countOfAtoms = function (formula) {
const getCount = (formula, factor = 1) => {
const n = formula.length;
const cnt = {};
const s = [];
let [atom, c] = ['', 0];

for (let i = 0; i <= n; i++) {
if (formula[i] === '(') {
const stk = ['('];
let j = i;
while (stk.length) {
j++;
if (formula[j] === '(') stk.push('(');
else if (formula[j] === ')') stk.pop();
}

const molecule = formula.slice(i + 1, j);
const nextFactor = [];

while (isDigit(formula[++j])) {
nextFactor.push(formula[j]);
}

const nextC = getCount(molecule, +nextFactor.join('') || 1);
for (const [atom, c] of Object.entries(nextC)) {
cnt[atom] = (cnt[atom] ?? 0) + c * factor;
}

i = j - 1;
continue;
}

if (s.length && (!formula[i] || isUpper(formula[i]))) {
[atom, c] = getAtom(s);

c *= factor;
cnt[atom] = (cnt[atom] ?? 0) + c;
s.length = 0;
}

s.push(formula[i]);
}

return cnt;
};

return Object.entries(getCount(formula))
.sort(([a], [b]) => a.localeCompare(b))
.map(([a, b]) => (b > 1 ? a + b : a))
.join('');
};

const regex = {
atom: /(\D+)(\d+)?/,
isUpper: /[A-Z]+/,
};
const getAtom = s => {
const [_, atom, c] = regex.atom.exec(s.join(''));
return [atom, c ? +c : 1];
};
const isDigit = ch => !Number.isNaN(Number.parseInt(ch));
const isUpper = ch => regex.isUpper.test(ch);
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
67 changes: 67 additions & 0 deletions solution/0700-0799/0726.Number of Atoms/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @param {string} formula
* @return {string}
*/
var countOfAtoms = function (formula) {
const getCount = (formula, factor = 1) => {
const n = formula.length;
const cnt = {};
const s = [];
let [atom, c] = ['', 0];

for (let i = 0; i <= n; i++) {
if (formula[i] === '(') {
const stk = ['('];
let j = i;
while (stk.length) {
j++;
if (formula[j] === '(') stk.push('(');
else if (formula[j] === ')') stk.pop();
}

const molecule = formula.slice(i + 1, j);
const nextFactor = [];

while (isDigit(formula[++j])) {
nextFactor.push(formula[j]);
}

const nextC = getCount(molecule, +nextFactor.join('') || 1);
for (const [atom, c] of Object.entries(nextC)) {
cnt[atom] = (cnt[atom] ?? 0) + c * factor;
}

i = j - 1;
continue;
}

if (s.length && (!formula[i] || isUpper(formula[i]))) {
[atom, c] = getAtom(s);

c *= factor;
cnt[atom] = (cnt[atom] ?? 0) + c;
s.length = 0;
}

s.push(formula[i]);
}

return cnt;
};

return Object.entries(getCount(formula))
.sort(([a], [b]) => a.localeCompare(b))
.map(([a, b]) => (b > 1 ? a + b : a))
.join('');
};

const regex = {
atom: /(\D+)(\d+)?/,
isUpper: /[A-Z]+/,
};
const getAtom = s => {
const [_, atom, c] = regex.atom.exec(s.join(''));
return [atom, c ? +c : 1];
};
const isDigit = ch => !Number.isNaN(Number.parseInt(ch));
const isUpper = ch => regex.isUpper.test(ch);
Loading