题解 | #查找兄弟单词#

查找兄弟单词

https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68

const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
rl.on("line"function (line) {
  handle(line);
});

function handle(line) {
  const arr = line.split(" ");
  const count = parseInt(arr[0]);
  const baseWord = arr[count + 1];
  const otherWords = arr.slice(1, count + 1);
  const k = arr[count + 2];

  const brotherArr = handleOtherWords(baseWord, otherWords);
  brotherArr.sort();
  console.log(brotherArr.length);
  brotherArr[k - 1&& console.log(brotherArr[k - 1]);
}

function handleOtherWords(baseWord, otherWords) {
  let brotherArr = [];
  otherWords.forEach((item) => {
    if (
      [...baseWord].sort().join(""=== [...item].sort().join(""&&
      baseWord !== item
    ) {
      brotherArr.push(item);
    }
  });

  return brotherArr;
}

#华为机试#
全部评论

相关推荐

uu们,拒offer时hr很生气怎么办我哭死
爱睡觉的冰箱哥:人家回收你的offer,或者oc后没给你发offer的时候可不会愧疚你,所以你拒了也没必要愧疚他。
点赞 评论 收藏
分享
程序员小白条:你是沟通了900个,不是投了900份简历,你能投900份,意味着对面都要回复你900次,你早就找到实习了,没亮点就是这样的,别局限地区,时间投的也要早,现在都要7月了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务