A happy string is a string that:
['a', 'b', 'c'].s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed).For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings.
Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order.
Return the kth string of this list or return an empty string if there are less than k happy strings of length n.
Example 1:
Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c".
Example 2:
Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1.
Example 3:
Input: n = 3, k = 9 Output: "cab" 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 9th string = "cab"
Constraints:
1 <= n <= 101 <= k <= 100This approach uses backtracking to generate all happy strings of the specified length n. The basic idea is to build strings character by character, ensuring at each step that the string remains 'happy' by not placing the same character consecutively. Once we have built a string of length n, we check if it is the k-th string that satisfies the conditions. This approach helps efficiently generate and count possible strings.
In the C solution, we use a recursive function generateHappyStrings to create strings of length n only using characters 'a', 'b', and 'c'. During recursion, we ensure a newly added character is different from the previous one. We use reference counting to track the number of valid happy strings constructed and compare it with k to check if we've reached the k-th string. If less than k strings are possible, an empty string is returned.
C++
Java
Python
C#
JavaScript
Time complexity is O(3^n) since each of the n positions can be filled in at most 3 ways. Space complexity is O(n) due to the recursion stack and storing strings up to length n.
An iterative approach can be used to generate strings by systematically constructing valid combinations of happy strings. Starting from 'a', 'b', 'c', you extend each string by appending characters that are different from the last one. Use a queue or list data structure for an efficient breadth-first construction of each happy string, and stop when the k-th string is constructed.
The C implementation uses a queue to iteratively build and explore possible happy strings. Starting with single characters, it appends each viable option until a string of desired length is achieved. It tracks the construction using breadth-first logic to monitor each level's string completion. A counter helps ascertain when the k-th valid configuration is reached, extracting and returning it thereafter. If fewer than k possible strings exist, an empty string is produced.
C++
Java
Python
C#
JavaScript
Time complexity is O(3^n) due to queue-based string generation. Space complexity is also O(3^n) for queue storage of possible strings during construction.
| Approach | Complexity |
|---|---|
| Backtracking Approach | Time complexity is |
| Iterative Enumeration | Time complexity is |
The k-th Lexicographical String of All Happy Strings of Length n - Leetcode 1415 - Python • NeetCodeIO • 8,327 views views
Watch 9 more video solutions →Practice The k-th Lexicographical String of All Happy Strings of Length n with our built-in code editor and test cases.
Practice on FleetCode