1415. The k-th Lexicographical String of All Happy Strings of Length n Medium

@problem@discussion
#String#Backtracking



1/**
2 * [1415] The k-th Lexicographical String of All Happy Strings of Length n
3 *
4 * A happy string is a string that:
5 * 
6 * 	consists only of letters of the set ['a', 'b', 'c'].
7 * 	s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed).
8 * 
9 * For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings.
10 * Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order.
11 * Return the kth string of this list or return an empty string if there are less than k happy strings of length n.
12 *  
13 * Example 1:
14 * 
15 * Input: n = 1, k = 3
16 * Output: "c"
17 * Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c".
18 * 
19 * Example 2:
20 * 
21 * Input: n = 1, k = 4
22 * Output: ""
23 * Explanation: There are only 3 happy strings of length 1.
24 * 
25 * Example 3:
26 * 
27 * Input: n = 3, k = 9
28 * Output: "cab"
29 * 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 9^th string = "cab"
30 * 
31 *  
32 * Constraints:
33 * 
34 * 	1 <= n <= 10
35 * 	1 <= k <= 100
36 * 
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/the-k-th-lexicographical-string-of-all-happy-strings-of-length-n/
41// discuss: https://leetcode.com/problems/the-k-th-lexicographical-string-of-all-happy-strings-of-length-n/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46    pub fn get_happy_string(n: i32, k: i32) -> String {
47        String::new()
48    }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_1415() {
59    }
60}
61


Back
© 2025 bowen.ge All Rights Reserved.