题解 | #查找兄弟单词#

查找兄弟单词

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

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
const judgeIsSublings = (a, b) => {
    if (a.length !== b.length || a === b) return false
    const sortA = [...a].sort().join('')
    const sortB = [...b].sort().join('')
    return sortA === sortB
}
rl.on('line', function (line) {
    const wordsArr = line.split(' ');
    const k = wordsArr.pop()
    const template = wordsArr.pop()
    wordsArr.shift()
    const sublingsArr = []
    wordsArr.forEach(word => {
        if (judgeIsSublings(word, template)) sublingsArr.push(word)
    })
    sublingsArr.sort()
    const count = sublingsArr.length
    console.log(count)
    if (k <= count) console.log(sublingsArr[k - 1])
});

全部评论

相关推荐

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