-
Notifications
You must be signed in to change notification settings - Fork 0
/
TheK-thLexicographicalStringOfAllHappyStringsOfLengthN.py
56 lines (49 loc) · 1.35 KB
/
TheK-thLexicographicalStringOfAllHappyStringsOfLengthN.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Solution:
n = 0
k = 0
str = []
ans = ['']
def recur(self, i):
if i == self.n:
self.k -= 1
if self.k == 0:
self.ans = list(self.str)
return
if i == 0:
self.str.append('a')
self.recur(i+1)
del self.str[-1]
self.str.append('b')
self.recur(i+1)
del self.str[-1]
self.str.append('c')
self.recur(i+1)
del self.str[-1]
elif self.str[i-1] == 'a':
self.str.append('b')
self.recur(i+1)
del self.str[-1]
self.str.append('c')
self.recur(i+1)
del self.str[-1]
elif self.str[i-1] == 'b':
self.str.append('a')
self.recur(i+1)
del self.str[-1]
self.str.append('c')
self.recur(i+1)
del self.str[-1]
else:
self.str.append('a')
self.recur(i+1)
del self.str[-1]
self.str.append('b')
self.recur(i+1)
del self.str[-1]
return
def getHappyString(self, n: int, k: int) -> str:
self.n = n
self.k = k
self.recur(0)
ans_str = ''.join(self.ans)
return ans_str