Skip to content

Commit e505245

Browse files
committed
Add solution and test-cases for problem 1415
1 parent d569e0c commit e505245

File tree

3 files changed

+84
-12
lines changed

3 files changed

+84
-12
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# [1415.The k-th Lexicographical String of All Happy Strings of Length n][title]
2+
3+
## Description
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+
11+
Given two integers `n` and `k`, consider a list of all happy strings of length `n` sorted in lexicographical order.
12+
13+
Return the kth string of this list or return an **empty string** if there are less than k happy strings of length n.
14+
15+
**Example 1:**
16+
17+
```
18+
Input: n = 1, k = 3
19+
Output: "c"
20+
Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c".
21+
```
22+
23+
**Example 2:**
24+
25+
```
26+
Input: n = 1, k = 4
27+
Output: ""
28+
Explanation: There are only 3 happy strings of length 1.
29+
```
30+
31+
**Example 3:**
32+
33+
```
34+
Input: n = 3, k = 9
35+
Output: "cab"
36+
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"
37+
```
38+
39+
## 结语
40+
41+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
42+
43+
[title]: https://leetcode.com/problems/the-k-th-lexicographical-string-of-all-happy-strings-of-length-n
44+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
var canUse = []byte{'a', 'b', 'c'}
4+
5+
func Solution(n int, k int) string {
6+
bs := make([]byte, n)
7+
var dfs func([]byte, byte, int)
8+
cnt := 0
9+
dfs = func(cur []byte, pre byte, index int) {
10+
if cnt > k {
11+
return
12+
}
13+
if index == n {
14+
cnt++
15+
if cnt == k {
16+
copy(bs, cur)
17+
}
18+
return
19+
}
20+
for _, b := range canUse {
21+
if b != pre {
22+
cur[index] = b
23+
dfs(cur, b, index+1)
24+
}
25+
}
26+
}
27+
arr := make([]byte, n)
28+
dfs(arr, byte(' '), 0)
29+
if cnt < k {
30+
return ""
31+
}
32+
return string(bs)
533
}

leetcode/1401-1500/1415.The-k-th-Lexicographical-String-of-All-Happy-Strings-of-Length-n/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
n, k int
14+
expect string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 1, 3, "c"},
17+
{"TestCase2", 1, 4, ""},
18+
{"TestCase3", 3, 9, "cab"},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.n, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.n, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)