Lexicographically Smallest Happy String of Length n With Letter Limits

Quick Overview

Build the lexicographically smallest string of length n over the letters a, b and c that never has three equal letters in a row and stays within a given limit for each letter. It tests building an answer one character at a time and recovering when an early choice leads to a dead end.

Lexicographically Smallest Happy String of Length n With Letter Limits

Company: DRW

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A string is called happy if it meets all of these conditions: - It contains only the letters `'a'`, `'b'` and `'c'`. - It does not contain `"aaa"`, `"bbb"` or `"ccc"` as a substring. - It contains at most `a` occurrences of the letter `'a'`. - It contains at most `b` occurrences of the letter `'b'`. - It contains at most `c` occurrences of the letter `'c'`. Given four integers `n`, `a`, `b` and `c`, return the lexicographically smallest happy string of length `n`. If no such string exists, return the empty string `""`. A string `x` is lexicographically smaller than a string `y` if, at the first position where `x` and `y` differ, `x` has a letter that appears earlier in the alphabet than the corresponding letter in `y`. ### Function Signature ```python def smallest_happy_string(n: int, a: int, b: int, c: int) -> str: ... ``` ### Rules - The returned string must have length exactly `n`. A shorter happy string does not count. - You do not have to use every available letter. The counts are upper limits. - Every candidate has the same length, so the comparison above picks exactly one answer. ### Constraints - `1 <= n <= 10` - `0 <= a, b, c <= 100` - `a + b + c > 0` ### Examples ```text Input: n = 3, a = 1, b = 1, c = 7 Output: "abc" ``` There is only one `'a'` and one `'b'`, so no length-3 string can start with `"aa"` or `"abb"`. The smallest choice at each position gives `"abc"`, which has no run of three equal letters. ```text Input: n = 7, a = 2, b = 2, c = 2 Output: "" ``` Only 6 letters are available in total (2 + 2 + 2), so no happy string of length 7 exists. ```text Input: n = 6, a = 2, b = 0, c = 4 Output: "accacc" ``` The limits add up to exactly 6, so the answer uses two `'a'` and four `'c'`. Any string starting with `"aa"` leaves `"cccc"` for the end, and `"aca"` leaves `"ccc"`, so the earliest prefix that can be completed is `"acc"`, followed by `"a"` and `"cc"`.

Overview: Build the lexicographically smallest string of length n over the letters a, b and c that never has three equal letters in a row and stays within a given limit for each letter. It tests building an answer one character at a time and recovering when an early choice leads to a dead end.

DRW logo
DRW
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

A string is called happy if it meets all of these conditions:

  • It contains only the letters 'a' , 'b' and 'c' .
  • It does not contain "aaa" , "bbb" or "ccc" as a substring.
  • It contains at most a occurrences of the letter 'a' .
  • It contains at most b occurrences of the letter 'b' .
  • It contains at most c occurrences of the letter 'c' .

Given four integers n, a, b and c, return the lexicographically smallest happy string of length n. If no such string exists, return the empty string "".

A string x is lexicographically smaller than a string y if, at the first position where x and y differ, x has a letter that appears earlier in the alphabet than the corresponding letter in y.

Function Signature

def smallest_happy_string(n: int, a: int, b: int, c: int) -> str:
    ...

Rules

  • The returned string must have length exactly n . A shorter happy string does not count.
  • You do not have to use every available letter. The counts are upper limits.
  • Every candidate has the same length, so the comparison above picks exactly one answer.

Constraints

  • 1 <= n <= 10
  • 0 <= a, b, c <= 100
  • a + b + c > 0

Examples

Input: n = 3, a = 1, b = 1, c = 7
Output: "abc"

There is only one 'a' and one 'b', so no length-3 string can start with "aa" or "abb". The smallest choice at each position gives "abc", which has no run of three equal letters.

Input: n = 7, a = 2, b = 2, c = 2
Output: ""

Only 6 letters are available in total (2 + 2 + 2), so no happy string of length 7 exists.

Input: n = 6, a = 2, b = 0, c = 4
Output: "accacc"

The limits add up to exactly 6, so the answer uses two 'a' and four 'c'. Any string starting with "aa" leaves "cccc" for the end, and "aca" leaves "ccc", so the earliest prefix that can be completed is "acc", followed by "a" and "cc".