diff --git a/solution/0700-0799/0726.Number of Atoms/README.md b/solution/0700-0799/0726.Number of Atoms/README.md index 2315f6e95b8de..898cfd3980a3c 100644 --- a/solution/0700-0799/0726.Number of Atoms/README.md +++ b/solution/0700-0799/0726.Number of Atoms/README.md @@ -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 = {}; + 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); +``` + diff --git a/solution/0700-0799/0726.Number of Atoms/README_EN.md b/solution/0700-0799/0726.Number of Atoms/README_EN.md index 76917f0343ba2..c0c48f50c6177 100644 --- a/solution/0700-0799/0726.Number of Atoms/README_EN.md +++ b/solution/0700-0799/0726.Number of Atoms/README_EN.md @@ -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 = {}; + 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); +``` + diff --git a/solution/0700-0799/0726.Number of Atoms/Solution.js b/solution/0700-0799/0726.Number of Atoms/Solution.js new file mode 100644 index 0000000000000..726bf59ac4d21 --- /dev/null +++ b/solution/0700-0799/0726.Number of Atoms/Solution.js @@ -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); diff --git a/solution/0700-0799/0726.Number of Atoms/Solution.ts b/solution/0700-0799/0726.Number of Atoms/Solution.ts new file mode 100644 index 0000000000000..4ea850eb92a09 --- /dev/null +++ b/solution/0700-0799/0726.Number of Atoms/Solution.ts @@ -0,0 +1,63 @@ +function countOfAtoms(formula: string): string { + const getCount = (formula: string, factor = 1) => { + const n = formula.length; + const cnt: Record = {}; + 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);