题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
let arr = readline().split(' ')
let n = Number(arr[0])
let words = arr.slice(1,n+1)
let x = arr[n+1]
let k = arr[n+2]
let tmparr = []
for(let i=0;i<n;i++){
if(isBrother(words[i],x)){
tmparr.push(words[i])
}
}
// 按字典排序
tmparr.sort()
// 打印输出
print(tmparr.length)
if(tmparr[k-1]){
print(tmparr[k-1])
}
// 判断是否兄弟单词
function isBrother(a,b){
if(!(a===b)){
if(a.length === b.length){
let len = a.length
let obj_a = {},obj_b={}
for(let i=0;i<len;i++){
obj_a[a[i]] = obj_a[a[i]]+1 || 1
obj_b[b[i]] = obj_b[b[i]]+1 || 1
}
// Object.assign(T,U) 将对象的属性序列调为一致 T目标对象 U原对象
return JSON.stringify(obj_a) === JSON.stringify(Object.assign(obj_a,obj_b))
}else{
return false
}
}else{
return false
}
}
let n = Number(arr[0])
let words = arr.slice(1,n+1)
let x = arr[n+1]
let k = arr[n+2]
let tmparr = []
for(let i=0;i<n;i++){
if(isBrother(words[i],x)){
tmparr.push(words[i])
}
}
// 按字典排序
tmparr.sort()
// 打印输出
print(tmparr.length)
if(tmparr[k-1]){
print(tmparr[k-1])
}
// 判断是否兄弟单词
function isBrother(a,b){
if(!(a===b)){
if(a.length === b.length){
let len = a.length
let obj_a = {},obj_b={}
for(let i=0;i<len;i++){
obj_a[a[i]] = obj_a[a[i]]+1 || 1
obj_b[b[i]] = obj_b[b[i]]+1 || 1
}
// Object.assign(T,U) 将对象的属性序列调为一致 T目标对象 U原对象
return JSON.stringify(obj_a) === JSON.stringify(Object.assign(obj_a,obj_b))
}else{
return false
}
}else{
return false
}
}