题解 | #查找兄弟单词#

查找兄弟单词

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 arra = line.split(' ');
        // 单词个数
        const num = parseInt(arra[0]);
        // 对比的单词
        const word = arra[num + 1];
        // 兄弟单词中需要取出的序列
        const sequence = arra[num + 2];
        // 所有的单词
        const totalWords = arra.slice(1, num);
        // 对单词进行排序
        const wordStr = word.split('').sort().join('');

        // 兄弟单词
        const brotherWords = totalWords.filter(c => {
            // 排除相等的单词 并且排序之后一样的单词
            return word != c && c.split('').sort().join('') == wordStr;
        });
        brotherWords.sort();

        console.log(brotherWords.length);
        if (brotherWords.length >= sequence) {
            console.log(brotherWords[sequence - 1]);
        }
    }
}()

全部评论

相关推荐

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