题解 | #查找兄弟单词#
查找兄弟单词
http://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
const input = readline().split(' ');
const n = Number(input.shift());
const k = Number(input.pop());
const x = input.pop();
function sortCallback(a, b) {
if (a < b) {
return -1;
}
return 1;
}
// 判断是否为兄弟单词
function isBrothers(str1, str2) {
// 长度不等不是兄弟单词
if (str1.length !== str2.length) {
return false;
}
// 完全相等不是兄弟单词
if (str1 === str2) {
return false;
}
const arr1 = str1.split('');
const arr2 = str2.split('');
arr1.sort(sortCallback);
arr2.sort(sortCallback);
// 所含字符完全以及每个字符的个数完全相等,才是兄弟单词
return arr1.join('') === arr2.join('');
}
// 保存兄弟单词
const brothers = [];
for (let i = 0; i < n; i++) {
if (isBrothers(input[i], x)) {
brothers.push(input[i]);
}
}
// 按字典序排序
brothers.sort(sortCallback);
console.log(brothers.length);
if (k < brothers.length) {
console.log(brothers[k - 1]);
}

