题解 | #查找兄弟单词#
查找兄弟单词
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())) { let arr = line.split(" "); let word_num = parseInt(arr[0]); let word_list = []; for (let i = 1; i <= word_num; i++) { word_list.push(arr[i]); } let target_word = arr[arr.length - 2]; let k = parseInt(arr[arr.length - 1]); let sibling_words = []; for (let i = 0; i < word_list.length; i++) { if ( word_list[i] != target_word && word_list[i].split("").sort().join() == target_word.split("").sort().join() ) { sibling_words.push(word_list[i]); } } sibling_words.sort(); // 按字典顺序排序 sort对字母默认升序排序,不需要传函数参数,数字排序才需要传函数参数 console.log(sibling_words.length); if (k >= 1 && k <= sibling_words.length) { console.log(sibling_words[k - 1]); } } })();