题解 | #查找兄弟单词#
查找兄弟单词
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())) { line = line.split(" "); let n = parseInt(line[0]); let x = line[line.length - 2]; let k = line[line.length - 1]; // 计算x中每个字母存在的次数 let xObj = {}; for (const val of x) { if (!xObj[val]) { xObj[val] = 1; } else { xObj[val]++; } } // 交换该单词字母顺序,不添加、删除、修改原有的字母,就是长度和原来的单词的长度相同 // 兄弟单词要求和原来的单词不同 let dicSingleWord = line .slice(1, line.length - 2) .filter((temp) => temp.length === x.length && temp !== x); let brotherWord = []; for (let i = 0; i < dicSingleWord.length; i++) { const temp = dicSingleWord[i]; // 查看单词的每个字母是否都在字典单词中 // 需要计算看当前单词中字符和x中每个字符的个数是否一致 let flag = x.split("").every((v) => { if ( temp.indexOf(v) != -1 && temp.split("").filter((e) => e == v).length === xObj[v] ) { return true; } return false; }); if (flag) { brotherWord.push(temp); } } brotherWord = brotherWord.sort(); console.log(brotherWord.length); console.log(brotherWord[k - 1] ? brotherWord[k - 1] : ""); } })();#查找兄弟单词#