题解 | #查找兄弟单词#

查找兄弟单词

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

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    // Write your code here
    while(line = await readline()){
        const inputs = line.split(" ");
        const n = inputs[0]
        const words = inputs.slice(1, n+1)
        const x = inputs.at(-2)
        const k = inputs.at(-1)

        const bros = []
        for (const w of words) {
            if (isBro(w, x)) {
                bros.push(w);
            }
        }
        console.log(bros.length);
        if (bros.length > k - 1) {
            bros.sort();
            console.log(bros[k-1])
        }
    }
}()

function isBro(a, b) {
    if (a.length !== b.length) {
        return false
    }
    if (a === b) {
        return false
    }
    return a.split("").sort().join("") === b.split("").sort().join("");
}

全部评论

相关推荐

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