题解 | #查找兄弟单词#
查找兄弟单词
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())) {
getResult(line);
}
})();
function getResult(data) {
let arr = data.split(" ");
let k = arr[Number(arr[0]) + 2];
let targetStr = arr[Number(arr[0]) + 1];
let resArr = [];
for (let i = 1; i < arr.length - 2; i++) {
if (isBrother(targetStr, arr[i])) {
resArr.push(arr[i]);
}
}
let sorted = resArr.sort((a, b) => {
for (let i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
return a[i].charCodeAt(0) - b[i].charCodeAt(0);
}
}
});
console.log(sorted.length);
if(sorted.length >= k){
console.log(sorted[k-1])
}
}
function isBrother(a, b) {
if (a == b || a.length != b.length) {
return false;
}
{
return (
a
.split("")
.sort((i, j) => i.charCodeAt(0) - j.charCodeAt(0))
.join("") ==
b
.split("")
.sort((i, j) => i.charCodeAt(0) - j.charCodeAt(0))
.join("")
);
}
}