题目:
思路:
并不需要先列出所有的兄弟单词
给了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)
}
}