const str = '25525511135'
// 暴力求解
const fn = (str) => {
let res = []
for (let i = 0; i < str.length - 3; i++) {
let p1 = str.substring(0, i + 1)
if (!isValidate(p1)) {
continue
}
for (let j = i + 1; j < str.length - 2; j++) {
let p2 = str.substring(i + 1, j + 1)
if (!isValidate(p2)) {
continue
}
for (let k = j + 1; k < str.length - 1; k++) {
let p3 = str.substring(j + 1, k + 1)
let p4 = str.substring(k + 1, str.length)
if (!isValidate(p3) || !isValidate(p4)) {
continue
}
res.push(`${p1}.${p2}.${p3}.${p4}`)
}
}
}
console.log(res)
}
// 验证是否合法
const isValidate = (str) => {
if (str.length >= 4 || (str.length > 1 && str.charAt(0) === 0) || str > 255) {
return false
}
return true
}
fn(str)
console