# 14.03.2026 [1415. The k-th Lexicographical String of All Happy Strings of Length n]
Автор: KittyCat, Keyboard and LeetCode
Загружено: 2026-03-14
Просмотров: 5
Описание:
14.03.2026
[1415. The k-th Lexicographical String of All Happy Strings of Length n](https://leetcode.com/problems/the-k-t...) medium
[blog post](https://leetcode.com/problems/the-k-t...)
[substack](https://open.substack.com/pub/dmitrii...)
[youtube]( • # 14.03.2026 [1415. The k-th Lexicographic... )

#### Join me on Telegram
https://t.me/leetcode_daily_unstoppab...
#### Problem TLDR
K-th generated abc non-repeating string #medium #dfs
#### Intuition
```j
// the problem is small, just generate all, sort
// abab
// abac
// abca
// abcb
// acab
// acac
// baba
// 2^10
```
The simple solution: just generate all strings in DFS, sort.
#### Approach
the DFS already generates in a sorted order, no need to sort
no need to keep strings, just count until k
the math intuition: each char generates subtree with known elements count, initial a: 2^(n-1), b: 2^(n-1), c: 2^(n-1)
index is "abc"[k/count] at first, then {"ab", "bc", "ac"}[k/count], k %= count, count /= 2
#### Complexity
Time complexity:
$$O(2^n)$$, or O(n)
Space complexity:
$$O(2^n)$$, or O(1)
#### Code
https://dmitrysamoylenko.com/2023/07/...
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: