/**
function multipleToken(tokenStr, multiple) { return `${tokenStr.substr(0,1)}${tokenStr.substr(1)*multiple}` } // tokens array function flatToken(tokens) { const flatToken = [] function isOverFlag(str) { return /\)/.test(str) } for (let i = 0; i < tokens.length; i++) { if (isOverFlag(tokens[i])) { let newToken = [] let popToken = flatToken.pop() while (popToken != '(') { newToken.push(multipleToken(popToken, tokens[i].substr(1))) popToken = flatToken.pop() } flatToken.push(...newToken) } else { flatToken.push(tokens[i]) } } return flatToken } function getValueFromFlatToken(flatToken) { const obj = {} flatToken.forEach((item) => { if (!obj[item.substr(0, 1)]) obj[item.substr(0, 1)] = 0 obj[item.substr(0, 1)] += (+item.substr(1) || 1) }) return obj } function prase(str) { const tokens = [] let tokenStr = '' function emitToken() { if (tokenStr) { if (tokenStr.length == 1) tokenStr += '1' tokens.push(tokenStr) tokenStr = '' } } for (let i = 0; i < str.length; i++) { const letter = str.substr(i, 1) if (letter == '(') { emitToken() tokens.push('(') continue } if (letter == ')') { emitToken() tokenStr += letter continue } const isLetter = (s) => !/[^A-Za-z]/.test(s) if (isLetter(letter)) { emitToken() tokenStr += letter continue } tokenStr += letter } emitToken() return tokens } function countOfLetters(letters) { // prase // flatToken // getValueFromFlatToken return getValueFromFlatToken(flatToken(prase(letters))) }