度小满9.20前端笔试 算法题AK答案,JS写的,供学习使用
1.涂色
function fn(own, need) {
const arr1 = own.split('')
const arr2 = need.split('')
let ret = 0
arr2.forEach(item => {
const index = arr1.indexOf(item)
if (index > -1) {
ret++
arr1[index] = null
}
})
return ret
} 2.相似字符串 function fn(str) {
if ((str.length % 3)) return false
let lastStr = ''
let tmp = ''
for (let i = 0; i < str.length; i++) {
tmp += str[i]
if ( !((i + 1) % 3) && i ) {
if (lastStr) {
let num = 0
for (let j = 0; j < 3; j++) {
lastStr[j] !== tmp[j] && num++
}
if (num > 1) return false
}
lastStr = tmp
tmp = ''
}
}
return true
} 