Interview Questions/Coding/Stable Alert Code

Stable Alert Code

Preview mode. Log in to edit, run, submit, and save progress.

Hard

Stable Alert Code

An alert encoder must build a string using exactly alertsA copies of the character a and exactly alertsB copies of the character b. The encoded string is rejected if it contains aaa or bbb anywhere as a contiguous substring. Among all accepted strings that use every required character, the encoder must choose the lexicographically smallest one. The test data guarantees that at least one accepted string exists. Input Format: You are given two integers alertsA and alertsB. Output Format: Return the lexicographically smallest stable alert code.

Examples

Example 1
Input:
alertsA = 1
alertsB = 2
Output:
abb

Explanation: "abb" uses 1 a-alert(s) and 2 b-alert(s), matching the requested counts. It contains no "aaa" or "bbb", so it is stable.

Approach hint

A character with count x can be split into slots around the other character.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp