题解 | #名字的漂亮度#

名字的漂亮度

https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

interface BeatyStr {
  str: string;
  times: number;
}

const lines: Array<string[]> = [];

rl.on('line', function (line: string) {
  lines.push(line.split(''));
});

rl.on('close', () => {
  lines.forEach((item, index) => {
    if (index > 0) {
      console.log(getBeaty(item));
    }
  });
});

const getBeaty = (strs: string[]): number => {
  let beatyStrs: BeatyStr[] = [];
  let maxBeaty = 0;

  strs.forEach((item) => {
    const index = beatyStrs.findIndex((i) => i.str === item);
    if (index > -1) {
      beatyStrs[index].times += 1;
    } else {
      beatyStrs.push({
        str: item,
        times: 1,
      });
    }
  });

  beatyStrs.sort((a, b) => b.times - a.times);

  beatyStrs.forEach((item, index) => {
    maxBeaty += item.times * (26 - index);
  })

  return maxBeaty;
};

全部评论

相关推荐

头像
03-30 21:02
已编辑
武汉大学 Java
ALEX_BLX:虽然说聊天记录不可信,不过这个趋势确实如此但我觉得也要想到一点就是卷后端的人里真正有“料”的人又有多少,我说的这个料都不是说一定要到大佬那种级别,而是就一个正常的水平。即使是现在也有很多人是跟风转码的,2-3个月速成后端技术栈的人数不胜数,但今时不同往日没可能靠速成进大厂了。这种情况就跟考研一样,你能上考场就已经打败一半的人了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务