From e2a9a60474ac4d91aa98c48bfffcf4b548a5f2e9 Mon Sep 17 00:00:00 2001 From: rain84 Date: Thu, 25 Jul 2024 06:46:43 +0300 Subject: [PATCH 1/2] feat: add ts solution to lc problem: No.0726 --- .../0700-0799/0726.Number of Atoms/README.md | 68 +++++++++++++++++++ .../0726.Number of Atoms/README_EN.md | 68 +++++++++++++++++++ .../0726.Number of Atoms/Solution.ts | 63 +++++++++++++++++ 3 files changed, 199 insertions(+) create mode 100644 solution/0700-0799/0726.Number of Atoms/Solution.ts diff --git a/solution/0700-0799/0726.Number of Atoms/README.md b/solution/0700-0799/0726.Number of Atoms/README.md index 2315f6e95b8de..4c655b66e5fa0 100644 --- a/solution/0700-0799/0726.Number of Atoms/README.md +++ b/solution/0700-0799/0726.Number of Atoms/README.md @@ -155,6 +155,74 @@ 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); +``` + 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..54b437cf33d77 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,74 @@ 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); +``` + 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); From e2125065ed85d8979e79c7be11b2ec108f203b5b Mon Sep 17 00:00:00 2001 From: rain84 Date: Thu, 25 Jul 2024 06:49:21 +0300 Subject: [PATCH 2/2] feat: add js solution to lc problem: No.0726 --- .../0700-0799/0726.Number of Atoms/README.md | 72 +++++++++++++++++++ .../0726.Number of Atoms/README_EN.md | 72 +++++++++++++++++++ .../0726.Number of Atoms/Solution.js | 67 +++++++++++++++++ 3 files changed, 211 insertions(+) create mode 100644 solution/0700-0799/0726.Number of Atoms/Solution.js diff --git a/solution/0700-0799/0726.Number of Atoms/README.md b/solution/0700-0799/0726.Number of Atoms/README.md index 4c655b66e5fa0..898cfd3980a3c 100644 --- a/solution/0700-0799/0726.Number of Atoms/README.md +++ b/solution/0700-0799/0726.Number of Atoms/README.md @@ -223,6 +223,78 @@ 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 54b437cf33d77..c0c48f50c6177 100644 --- a/solution/0700-0799/0726.Number of Atoms/README_EN.md +++ b/solution/0700-0799/0726.Number of Atoms/README_EN.md @@ -223,6 +223,78 @@ 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);