Skip to content

Commit 70be960

Browse files
committed
Sync LeetCode submission Runtime - 44 ms (40.92%), Memory - 17.9 MB (66.08%)
1 parent bbe7edd commit 70be960

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<p>A <strong>happy string</strong> is a string that:</p>
2+
3+
<ul>
4+
<li>consists only of letters of the set <code>[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]</code>.</li>
5+
<li><code>s[i] != s[i + 1]</code> for all values of <code>i</code> from <code>1</code> to <code>s.length - 1</code> (string is 1-indexed).</li>
6+
</ul>
7+
8+
<p>For example, strings <strong>&quot;abc&quot;, &quot;ac&quot;, &quot;b&quot;</strong> and <strong>&quot;abcbabcbcb&quot;</strong> are all happy strings and strings <strong>&quot;aa&quot;, &quot;baa&quot;</strong> and <strong>&quot;ababbc&quot;</strong> are not happy strings.</p>
9+
10+
<p>Given two integers <code>n</code> and <code>k</code>, consider a list of all happy strings of length <code>n</code> sorted in lexicographical order.</p>
11+
12+
<p>Return <em>the kth string</em> of this list or return an <strong>empty string</strong> if there are less than <code>k</code> happy strings of length <code>n</code>.</p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong class="example">Example 1:</strong></p>
16+
17+
<pre>
18+
<strong>Input:</strong> n = 1, k = 3
19+
<strong>Output:</strong> &quot;c&quot;
20+
<strong>Explanation:</strong> The list [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;] contains all happy strings of length 1. The third string is &quot;c&quot;.
21+
</pre>
22+
23+
<p><strong class="example">Example 2:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> n = 1, k = 4
27+
<strong>Output:</strong> &quot;&quot;
28+
<strong>Explanation:</strong> There are only 3 happy strings of length 1.
29+
</pre>
30+
31+
<p><strong class="example">Example 3:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong> n = 3, k = 9
35+
<strong>Output:</strong> &quot;cab&quot;
36+
<strong>Explanation:</strong> There are 12 different happy string of length 3 [&quot;aba&quot;, &quot;abc&quot;, &quot;aca&quot;, &quot;acb&quot;, &quot;bab&quot;, &quot;bac&quot;, &quot;bca&quot;, &quot;bcb&quot;, &quot;cab&quot;, &quot;cac&quot;, &quot;cba&quot;, &quot;cbc&quot;]. You will find the 9<sup>th</sup> string = &quot;cab&quot;
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
<p><strong>Constraints:</strong></p>
41+
42+
<ul>
43+
<li><code>1 &lt;= n &lt;= 10</code></li>
44+
<li><code>1 &lt;= k &lt;= 100</code></li>
45+
</ul>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Approach 1: Backtracking
2+
3+
# Time: O(n * 2^n)
4+
# Space: O(2^n)
5+
6+
class Solution:
7+
def getHappyString(self, n: int, k: int) -> str:
8+
current_str = ''
9+
happy_strings = []
10+
11+
self.generate_happy_strings(n, current_str, happy_strings)
12+
13+
if len(happy_strings) < k:
14+
return ''
15+
16+
happy_strings.sort()
17+
return happy_strings[k - 1]
18+
19+
def generate_happy_strings(self, n, current_str, happy_strings):
20+
if len(current_str) == n:
21+
happy_strings.append(current_str)
22+
return
23+
24+
for current_char in ['a', 'b', 'c']:
25+
if len(current_str) > 0 and current_str[-1] == current_char:
26+
continue
27+
28+
self.generate_happy_strings(n, current_str + current_char, happy_strings)
29+

0 commit comments

Comments
 (0)