TypeScript Largest String
function largestString(s: string): string {
const chars = s.split("");
let cur = "";
let i = chars.length - 1;
while (i >= 0) {
cur = chars[i] + cur;
if (cur.length === 3) {
if (cur === "abb") {
chars[i] = "b";
chars[i + 1] = "a";
chars[i + 2] = "a";
if (chars[i + 4] !== undefined && chars[i + 4] === "b") {
i += 4 + 1;
} else if (chars[i + 3] !== undefined && chars[i + 3] === "b") {
i += 3 + 1;
} else if (chars[i + 2] === "b") {
i += 2 + 1;
}
}
if (chars[i + 1] === "b") {
i += 1 + 1;
} else {
i += 1;
}
cur = "";
}
i--;
}
return chars.join("");
}
This builds the biggest valid string it can under the challenge rules by always choosing the best next character it is allowed to use.