题解 | #查找兄弟单词#
查找兄弟单词
http://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
一个递归搞定,就是判断两个字符串 字母是否全部相等并且位置不一样 位置一样的剔除
let line
function diff(target,current){
if(target.length == 0) return true
let letter = target[0];
let index = current.indexOf(letter);
if(index == -1){
return false
}else{
current.splice(index,1)
target.splice(0,1)
return diff(target,current)
}
return true
}
function brotherWord(target,current){
if(target.length < 2) return false
if(target.length != current.length) return false
if(target == current) return false
return diff(target.split(''),current.split(''))
}
while(line = readline()){
let arr = line.split(' ');
let num = arr.splice(0,1)
let k = arr.splice(arr.length - 1,1)[0]
let word = arr.splice(arr.length - 1,1)[0];
let res = []
for(let i=0;i<arr.length;i++){
if(brotherWord(word,arr[i])){
res.push(arr[i])
}
}
res = res.sort();
console.log(res.length);
if(parseInt(k) < res.length){
console.log(res[parseInt(k) - 1])
}
}