The k-th Lexicographical String of All Happy Strings of Length n

Medium Topics Company Tags

A happy string uses only a, b, and c, and no two neighboring letters are equal. List all happy strings of length n in alphabetical (lexicographical) order. Return the string at the 1-based position k, or the empty string "" if the list has fewer than k entries.

Example 1:

Input: n = 2
k = 4

Output: "bc"

Explanation: The ordered list is ab, ac, ba, bc, ca, cb. Its fourth entry is bc.

Example 2:

Input: n = 2
k = 7

Output: ""

Explanation: There are only six happy strings of length 2, so a seventh entry does not exist.

Example 3:

Input: n = 1
k = 2

Output: "b"

Explanation: The one-letter list is a, b, c, so the second entry is b.

Constraints:

  • 1 <= n <= 10
  • 1 <= k <= 100


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.