题解 | #名字的漂亮度#
名字的漂亮度
https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
很漂亮的代码 object转array及object排序的运用 完整代码如下:
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; let lines = []; void async function () { // Write your code here while(line = await readline()){ lines.push(line); if (lines.length - 1 == parseInt(lines[0])) { for (let i = 1; i <= lines.length; i++) { console.log(maxBeauty(lines[i])); } } } }() function maxBeauty(str){ let arr = str.split(''); let object = arr.reduce((a, b) => ({...a, [b]: 1 + (a[b] = a[b] ? a[b] : 0)}), {}); let m = 26 let ordered = Object.entries(object).sort(([,a],[,b]) => b-a).reduce((r, [k, v]) => ({ ...r, [k]: m-- }), {}); let res = 0 for (let i = 0; i < str.length; i++){ res += ordered[str[i]] } return res; }