Skip to content

Commit 3507588

Browse files
committed
Add solution and test-cases for problem 2182
1 parent dbd4dda commit 3507588

File tree

3 files changed

+96
-25
lines changed

3 files changed

+96
-25
lines changed

leetcode/2101-2200/2182.Construct-String-With-Repeat-Limit/README.md

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
# [2182.Construct String With Repeat Limit][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a string `s` and an integer `repeatLimit`. Construct a new string `repeatLimitedString` using the characters of `s` such that no letter appears **more than** `repeatLimit` times **in a row**. You do **not** have to use all characters from `s`.
5+
6+
Return the **lexicographically largest** `repeatLimitedString` possible.
7+
8+
A string `a` is **lexicographically larger** than a string `b` if in the first position where `a` and `b` differ, string `a` has a letter that appears later in the alphabet than the corresponding letter in `b`. If the first `min(a.length, b.length)` characters do not differ, then the longer string is the lexicographically larger one.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: s = "cczazcc", repeatLimit = 3
14+
Output: "zzcccac"
15+
Explanation: We use all of the characters from s to construct the repeatLimitedString "zzcccac".
16+
The letter 'a' appears at most 1 time in a row.
17+
The letter 'c' appears at most 3 times in a row.
18+
The letter 'z' appears at most 2 times in a row.
19+
Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.
20+
The string is the lexicographically largest repeatLimitedString possible so we return "zzcccac".
21+
Note that the string "zzcccca" is lexicographically larger but the letter 'c' appears more than 3 times in a row, so it is not a valid repeatLimitedString.
1322
```
1423

15-
## 题意
16-
> ...
24+
**Example 2:**
1725

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Construct String With Repeat Limit
23-
```go
2426
```
25-
27+
Input: s = "aababab", repeatLimit = 2
28+
Output: "bbabaa"
29+
Explanation: We use only some of the characters from s to construct the repeatLimitedString "bbabaa".
30+
The letter 'a' appears at most 2 times in a row.
31+
The letter 'b' appears at most 2 times in a row.
32+
Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.
33+
The string is the lexicographically largest repeatLimitedString possible so we return "bbabaa".
34+
Note that the string "bbabaaa" is lexicographically larger but the letter 'a' appears more than 2 times in a row, so it is not a valid repeatLimitedString.
35+
```
2636

2737
## 结语
2838

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,66 @@
11
package Solution
22

3-
func Solution(x bool) bool {
3+
import (
4+
"bytes"
5+
"container/heap"
6+
)
7+
8+
type tmp2182 struct {
9+
b byte
10+
c int
11+
}
12+
type heap2182 []tmp2182
13+
14+
func (h *heap2182) Len() int {
15+
return len(*h)
16+
}
17+
18+
func (h *heap2182) Less(i, j int) bool {
19+
return (*h)[i].b > (*h)[j].b
20+
}
21+
22+
func (h *heap2182) Swap(i, j int) {
23+
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
24+
}
25+
26+
func (h *heap2182) Push(x any) {
27+
*h = append(*h, x.(tmp2182))
28+
}
29+
30+
func (h *heap2182) Pop() any {
31+
old := *h
32+
l := len(old)
33+
x := old[l-1]
34+
*h = old[:l-1]
435
return x
536
}
37+
38+
func Solution(s string, repeatLimit int) string {
39+
c := make(map[byte]int)
40+
for _, b := range []byte(s) {
41+
c[b]++
42+
}
43+
list := &heap2182{}
44+
for k, v := range c {
45+
heap.Push(list, tmp2182{k, v})
46+
}
47+
buf := bytes.NewBuffer([]byte{})
48+
49+
for list.Len() > 0 {
50+
top := heap.Pop(list).(tmp2182)
51+
for range min(top.c, repeatLimit) {
52+
buf.WriteByte(top.b)
53+
}
54+
top.c -= repeatLimit
55+
if top.c > 0 && list.Len() > 0 {
56+
nextPickOne := heap.Pop(list).(tmp2182)
57+
buf.WriteByte(nextPickOne.b)
58+
nextPickOne.c--
59+
if nextPickOne.c > 0 {
60+
heap.Push(list, nextPickOne)
61+
}
62+
heap.Push(list, top)
63+
}
64+
}
65+
return buf.String()
66+
}

leetcode/2101-2200/2182.Construct-String-With-Repeat-Limit/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+
s string
14+
limit int
15+
expect string
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "cczazcc", 3, "zzcccac"},
18+
{"TestCase2", "aababab", 2, "bbabaa"},
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.s, c.limit)
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.s, c.limit)
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)