Skip to content

Commit 1b2492a

Browse files
committed
Create file
1 parent 1ff5954 commit 1b2492a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const RELATIVE_SHIFT: [[usize;2];3] = [
2+
[1, 2],
3+
[0, 2],
4+
[0, 1]
5+
];
6+
7+
impl Solution {
8+
pub fn get_happy_string(n: i32, k: i32) -> String {
9+
if k > (1 << (n - 1)) * 3 {
10+
return String::new();
11+
}
12+
let mut result = Vec::with_capacity(n as usize);
13+
let mut combinations = 1_usize << (n - 1);
14+
let mut previous_shift = (k as usize - 1) / combinations;
15+
let mut k = (k as usize - 1) % combinations;
16+
result.push(previous_shift as u8 + b'a');
17+
for i in 0..n - 1 {
18+
combinations >>= 1;
19+
let shift = RELATIVE_SHIFT[previous_shift][k / combinations];
20+
result.push(shift as u8 + b'a');
21+
k %= combinations;
22+
previous_shift = shift;
23+
}
24+
unsafe{String::from_utf8_unchecked(result)}
25+
}
26+
}

0 commit comments

Comments
 (0)