const SURROGATE_PAIR_RE = /(?:[\ud800-\udbff][\udc00-\udfff])/;
function countCodePoints (s) {
const re = new RegExp(SURROGATE_PAIR_RE.source + "|[\\S\\s]", "g");
let n = 0;
while (re.exec(s))
n++;
return n;
}
function odai_pt15_467 (s) {
const re = new RegExp("(" + SURROGATE_PAIR_RE.source + "|[\\S\\s])\\1{1,}",
"g");
return s.replace(re, function (substring, character) {
if (substring.length == 1)
return substring;
else
return character + countCodePoints(substring);
});
}
for (let s of ["あいうえお",
"ああいいうう",
"あいうあいう",
"ああああ",
"🐤🐤🐤🐤"]) {
print(odai_pt15_467(s));
}
Y29uc3QgU1VSUk9HQVRFX1BBSVJfUkUgPSAvKD86W1x1ZDgwMC1cdWRiZmZdW1x1ZGMwMC1cdWRmZmZdKS87CgpmdW5jdGlvbiBjb3VudENvZGVQb2ludHMgKHMpIHsKICBjb25zdCByZSA9IG5ldyBSZWdFeHAoU1VSUk9HQVRFX1BBSVJfUkUuc291cmNlICsgInxbXFxTXFxzXSIsICJnIik7CiAgbGV0IG4gPSAwOwogIHdoaWxlIChyZS5leGVjKHMpKQogICAgbisrOwogIHJldHVybiBuOwp9CgpmdW5jdGlvbiBvZGFpX3B0MTVfNDY3IChzKSB7CiAgY29uc3QgcmUgPSBuZXcgUmVnRXhwKCIoIiArIFNVUlJPR0FURV9QQUlSX1JFLnNvdXJjZSArICJ8W1xcU1xcc10pXFwxezEsfSIsCiAgICAgICAgICAgICAgICAgICAgICAgICJnIik7CiAgcmV0dXJuIHMucmVwbGFjZShyZSwgZnVuY3Rpb24gKHN1YnN0cmluZywgY2hhcmFjdGVyKSB7CiAgICBpZiAoc3Vic3RyaW5nLmxlbmd0aCA9PSAxKQogICAgICByZXR1cm4gc3Vic3RyaW5nOwogICAgZWxzZQogICAgICByZXR1cm4gY2hhcmFjdGVyICsgY291bnRDb2RlUG9pbnRzKHN1YnN0cmluZyk7CiAgfSk7Cn0KCmZvciAobGV0IHMgb2YgWyLjgYLjgYTjgYbjgYjjgYoiLAogICAgICAgICAgICAgICAi44GC44GC44GE44GE44GG44GGIiwKICAgICAgICAgICAgICAgIuOBguOBhOOBhuOBguOBhOOBhiIsCiAgICAgICAgICAgICAgICLjgYLjgYLjgYLjgYIiLAogICAgICAgICAgICAgICAi8J+QpPCfkKTwn5Ck8J+QpCJdKSB7CiAgcHJpbnQob2RhaV9wdDE1XzQ2NyhzKSk7Cn0K