HJ27 查找兄弟单词

题目:
在这里插入图片描述
思路:
并不需要先列出所有的兄弟单词
给了6个单词就把每个单词打散排序作为字典key,这样就筛选出来比如:
{
abc: 【xxx,xxx】
ac:【xx, xx】
}
只需要将关键的单词作为key,在对象中找到并按要求输出即可,不需要他自己
提交:

while(line = readline()) {
    const [num, ...rest] = line.split(' ')
    const wordsLength = Number(num)
    const words = rest.slice(0, wordsLength)
    const keyWord = rest[wordsLength]
    const index = Number(rest[wordsLength + 1])
    const dictionary = {}
    words.forEach(item => {
        const currentKey = item.split('').sort().join('')
        const currentArr = dictionary[currentKey] || []
        dictionary[currentKey] = [...currentArr, item].sort()
    })
    if (dictionary[keyWord.split('').sort().join('')]) {
        const currentItem = dictionary[keyWord.split('').sort().join('')].filter(item => item !==keyWord)
        const result1 = currentItem.length
        const result2 = currentItem[index - 1]
        console.log(result1)
        if (result2) {
            console.log(result2)
        }
    } else {
        console.log(0)
    }
     
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值