File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ # _pm_dfs stands for premature dfs
3+ def _pm_dfs (self , rem : int , n : int , output : List [str ]) -> str :
4+ if not n :
5+ return '' .join (output )
6+
7+ pot_per_path = 2 ** (n - 1 )
8+ (options := ['a' , 'b' , 'c' ]).remove (output [- 1 ])
9+
10+ if rem <= pot_per_path :
11+ output .append (options [0 ])
12+ return self ._pm_dfs (rem , n - 1 , output )
13+ output .append (options [1 ])
14+ return self ._pm_dfs (rem - pot_per_path , n - 1 , output )
15+
16+ def getHappyString (self , n : int , k : int ) -> str :
17+ n -= 1
18+ pot_n_min_1 = 2 ** n
19+
20+ match (k - 1 ) // pot_n_min_1 :
21+ case 0 :
22+ return self ._pm_dfs (k , n , ['a' ])
23+ case 1 :
24+ return self ._pm_dfs (k - pot_n_min_1 , n , ['b' ])
25+ case 2 :
26+ return self ._pm_dfs (k - 2 * pot_n_min_1 , n , ['c' ])
27+ case _ :
28+ return ''
You can’t perform that action at this time.
0 commit comments