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 ofi
from1
tos.length - 1
(string is 1-indexed).
For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings.
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
.
Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c".
Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1.
Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab"
Input: n = 2, k = 7 Output: ""
Input: n = 10, k = 100 Output: "abacbabacb"
1 <= n <= 10
1 <= k <= 100
class Solution:
def getHappyString(self, n: int, k: int) -> str:
happy = list("abc")
for _ in range(n - 1):
happy_ = []
for s in happy:
for c in "abc":
if s[-1] != c:
happy_.append(s + c)
happy = happy_
return "" if k > len(happy) else happy[k - 1]
# @param {Integer} n
# @param {Integer} k
# @return {String}
def get_happy_string(n, k)
happy = %w[a b c]
(2..n).each do |_i|
happy_ = []
happy.each do |s|
'abc'.chars.each do |c|
happy_.push(s + c) if s[-1] != c
end
end
happy = happy_
end
k > happy.size ? '' : happy[k - 1]
end