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

A happy string is a string that:

  • Consists only of letters of the set ['a', 'b', 'c'].
  • s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed).

Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order.

Return the kth string of this list, or return an empty string if there are less than k happy strings of length n.

Example 1
Inputn = 1, k = 3
Output"c"
The list ["a", "b", "c"] contains all happy strings of length 1, and the third string is "c".
Example 2
Inputn = 1, k = 4
Output""
There are only 3 happy strings of length 1.

Constraints

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

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate