题解 | #查找兄弟单词#

查找兄弟单词

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


const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
rl.on('line', function (line) {
    const arr = line.split(" ");
    const len = arr.shift();
    const k = arr.pop();
    const keyWord = arr.pop();
    const siblings = [];
    arr.forEach(word => {
        if (word === keyWord) {
            return;
        } 
        else if (word.length === keyWord.length) {
            const letters = [...word];
            const keyLetters = [...keyWord];
            letters.sort();
            keyLetters.sort();
            if (letters.join("") === keyLetters.join("")) {
                siblings.push(word);
            }
        }
    });
    siblings.sort();
    console.log(siblings.length);
    if (siblings.length > k) {
        console.log(siblings[k - 1]);
    }
});
全部评论

相关推荐

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