题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); rl.on("line", function (line: string) { const lines = line.split(" "); const k = parseInt(lines[lines.length - 1]); const x = lines[lines.length - 2]; const dictionary: string[] = []; lines.forEach((item, index) => { if ( index !== 0 && index !== lines.length - 1 && index !== lines.length - 2 ) { dictionary.push(item); } }); const brothers: string[] = []; dictionary.forEach((item) => { if (isBrother(x, item)) { brothers.push(item); } }); console.log(brothers.length); if (k <= brothers.length) { console.log(brothers.sort()[k - 1]); } }); // 是否兄弟单词 const isBrother = (x: string, char: string): boolean => { if (x.length !== char.length || x === char) { return false; } const sortX = x.split("").sort().join(""); const sortChar = char.split("").sort().join(""); return sortX === sortChar; };