1.判断字符串a是否包含字符串b
function isContain(a, b){ for( let i = 0; i<a.length; i++){ if(a[i] === b[0]){ let tempRe = true for(let j =0; j<b.length;j++){ let x = b[j] let y = a[i+j] if(x!=y){ tempRe = false break } } if(tempRe) { console.log(i) return } } } console.log(-1) }
isContain('1234355','43') // 3
2.找最大公共子字符串
findMaxSameStr(a,b) { let temp = new Array() let maxNum = 0, index = null for (let i=0; i<a.length; i++) { temp[i] = new Array() for (let j=0; j<b.length; j++) { if(a[i] === b[j]){ if(i>0&&j>0&&temp[i-1][j-1]>0) { temp[i][j] = 1+ temp[i-1][j-1] } else temp[i][j] = 1 if(maxNum < temp[i][j]){ maxNum = temp[i][j] index = i } } else temp[i][j] = 0 } } console.log(temp) console.log(index,maxNum) console.log(a.substr(index-maxNum+1, maxNum)) return a.substr(index-maxNum+1, maxNum) }
findMaxSameStr(a,b) {
let temp = new Array()
let maxNum = 0, index = null