The k-th Lexicographical String of All Happy Strings of Length n
https://leetcode.com/contest/biweekly-contest-24/problems/the-k-th-lexicographical-string-of-all-happy-strings-of-length-n/
这道题需要反复记忆,反之现场我是想不来,string permutation之类的是薄弱点。
permutation一般都可以用recursion来解决。
class Solution:
def getHappyString(self, n: int, k: int) -> str:
def generate(prev):
if len(prev) == n:
yield prev
return
for ch in "abc":
if not prev or prev[-1] != ch:
for s in generate(prev+ch):
yield s
for i,val in enumerate(generate(""),1):
if i == k:
return val
return ""
Previous1414. Find the Minimum Number of Fibonacci Numbers Whose Sum Is KNext17. Letter Combinations of a Phone Number
Last updated
Was this helpful?