题解 | #查找兄弟单词#

查找兄弟单词

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;
}

#华为机试#
全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务