Created
March 17, 2021 12:41
-
-
Save ekibun/ecf1d01904a46fd7836cc86d16a59842 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const P = "BC"; | |
const B = "AAABBBBCCCCBBB"; | |
console.log(`P=${P}\nB=${B}`); | |
function testB(b) { | |
if (!b) return 0; | |
if (b.length < 3) return -1; | |
let min = -2; | |
let s = 0; | |
for (let i = 1; i < b.length; ++i) { | |
if (b[s] != b[i]) { | |
if (i - s > 2) { | |
const rest = b.substring(0, s) + b.substring(i); | |
const c = testB(rest); | |
if (c > 0) | |
min = min == -2 || min > c ? c : min; | |
} | |
s = i; | |
} | |
} | |
if (s == 0) return 1; | |
return min + 1; | |
} | |
function testP(p, b) { | |
if (!p) return testB(b); | |
let min = -1; | |
for (let i = 0; i < b.length - p.length + 1; ++i) { | |
if (b[i] == p[0]) { | |
const left = testB(b.substring(0, i)) | |
const right = testP(p.substring(1), b.substring(i + 1)); | |
if (left >= 0 && right >= 0) { | |
const c = left + right; | |
console.log(`${c}=${left}+${right} ${b.substring(0, i)}-${b[i]}-${b.substring(i + 1)}`); | |
min = min == -1 || min > c ? c : min; | |
} | |
} | |
} | |
return min; | |
} | |
console.log(testP(P, B)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment