Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 104da8b

Browse files
committed
Merge pull request #386 from 0xff-dev/1374
Add solution and test-cases for problem 1374
2 parents 1ba7dd5 + 52aa830 commit 104da8b

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# [1374.Generate a String With Characters That Have Odd Counts][title]
2+
3+
## Description
4+
Given an integer `n`, return a string with `n` characters such that each character in such string occurs **an odd number of times**.
5+
6+
The returned string must contain only lowercase English letters. If there are multiples valid strings, return **any** of them.
7+
8+
**Example 1:**
9+
10+
```
11+
Input: n = 4
12+
Output: "pppz"
13+
Explanation: "pppz" is a valid string since the character 'p' occurs three times and the character 'z' occurs once. Note that there are many other valid strings such as "ohhh" and "love".
14+
```
15+
16+
**Example 2:**
17+
18+
```
19+
Input: n = 2
20+
Output: "xy"
21+
Explanation: "xy" is a valid string since the characters 'x' and 'y' occur once. Note that there are many other valid strings such as "ag" and "ur".
22+
```
23+
24+
25+
**Example 3:**
26+
27+
```
28+
Input: n = 7
29+
Output: "holasss"
30+
```
31+
32+
## 结语
33+
34+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
35+
36+
[title]: https://leetcode.com/problems/generate-a-string-with-characters-that-have-odd-counts
37+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package Solution
2+
3+
import "strings"
4+
5+
func Solution(n int) string {
6+
sb := strings.Builder{}
7+
even := n&1 == 0
8+
c := byte('a')
9+
if even {
10+
sb.WriteByte(c)
11+
c++
12+
n--
13+
}
14+
15+
for i := 0; i < n; i++ {
16+
sb.WriteByte(c)
17+
}
18+
return sb.String()
19+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"strconv"
6+
"testing"
7+
)
8+
9+
func TestSolution(t *testing.T) {
10+
// 测试用例
11+
cases := []struct {
12+
name string
13+
inputs int
14+
expect string
15+
}{
16+
{"TestCase1", 1, "a"},
17+
{"TestCase2", 2, "ab"},
18+
{"TestCase3", 88, "abbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"},
19+
}
20+
21+
// 开始测试
22+
for i, c := range cases {
23+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24+
got := Solution(c.inputs)
25+
if !reflect.DeepEqual(got, c.expect) {
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27+
c.expect, got, c.inputs)
28+
}
29+
})
30+
}
31+
}
32+
33+
// 压力测试
34+
func BenchmarkSolution(b *testing.B) {
35+
}
36+
37+
// 使用案列
38+
func ExampleSolution() {
39+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /