题解 | #查找兄弟单词#

查找兄弟单词

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) {
    let arr = line.split(" ");
    const num = Number(arr[0]);
    const x = arr[num + 1];
    const sortedX = x.split("").sort().join("");
    const index = arr[num + 2];
    let words = arr.slice(1, num + 1);
    let brothers = [];
    function isBrother(word) {
        if (word.length !== x.length || word === x) return false;
        return word.split("").sort().join("") === sortedX;
    }
    words.forEach((w) => isBrother(w) && brothers.push(w));
    brothers.sort();
    console.log(brothers.length);
    console.log(brothers[index-1]||'')
});

全部评论

相关推荐

头像
09-01 09:00
已编辑
四川旅游学院 运营
牛客55195891...:主要是专业不好,别的没毛病
牛客解忧铺
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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