Skip to content

Commit 51f36c0

Browse files
authored
Merge pull request #1245 from 0xff-dev/2014
Add solution and test-cases for problem 2014
2 parents c47043b + d2137d4 commit 51f36c0

File tree

4 files changed

+91
-26
lines changed

4 files changed

+91
-26
lines changed
5.16 KB
Loading

leetcode/2001-2100/2014.Longest-Subsequence-Repeated-k-Times/README.md

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
# [2014.Longest Subsequence Repeated k Times][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
74

8-
**Example 1:**
5+
You are given a string `s` of length `n`, and an integer `k`. You are tasked to find the **longest subsequence repeated** `k` times in string `s`.
6+
7+
A **subsequence** is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
8+
9+
A subsequence `seq` is **repeated** `k` times in the string `s` if `seq * k` is a subsequence of `s`, where `seq * k` represents a string constructed by concatenating `seq k` times.
10+
11+
- For example, `"bba"` is repeated `2` times in the string `"bababcba"`, because the string `"bbabba"`, constructed by concatenating `"bba"` `2` times, is a subsequence of the string `"bababcba"`.
12+
13+
Return the **longest subsequence repeated** `k` times in string `s`. If multiple such subsequences are found, return the **lexicographicall6 largest** one. If there is no such subsequence, return an **empty** string.
14+
15+
**Example 1:**
16+
17+
![1](./1.png)
918

1019
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
20+
Input: s = "letsleetcode", k = 2
21+
Output: "let"
22+
Explanation: There are two longest subsequences repeated 2 times: "let" and "ete".
23+
"let" is the lexicographically largest one.
1324
```
1425

15-
## 题意
16-
> ...
17-
18-
## 题解
26+
**Example 2:**
1927

20-
### 思路1
21-
> ...
22-
Longest Subsequence Repeated k Times
23-
```go
28+
```
29+
Input: s = "bb", k = 2
30+
Output: "b"
31+
Explanation: The longest subsequence repeated 2 times is "b".
2432
```
2533

34+
**Example 3:**
35+
36+
```
37+
Input: s = "ab", k = 2
38+
Output: ""
39+
Explanation: There is no subsequence repeated 2 times. Empty string is returned.
40+
```
2641

2742
## 结语
2843

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,54 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string, k int) string {
4+
count := [26]int{}
5+
for _, b := range s {
6+
count[b-'a']++
7+
}
8+
var greaterK []string
9+
for i := 25; i >= 0; i-- {
10+
if count[i] >= k {
11+
greaterK = append(greaterK, string('a'+byte(i)))
12+
}
13+
}
14+
15+
var match func(string) bool
16+
match = func(pattern string) bool {
17+
i, c := 0, 0
18+
for j := 0; j < len(s); j++ {
19+
if s[j] == pattern[i] {
20+
i++
21+
if i == len(pattern) {
22+
c++
23+
i = 0
24+
if c >= k {
25+
return true
26+
}
27+
}
28+
}
29+
}
30+
return false
31+
}
32+
33+
loop := make([]string, len(greaterK))
34+
copy(loop, greaterK)
35+
// z, y, x, 我们应该需要判断 z ok (zz) ok , zzz(ok) / 以及 zx, zy ...
36+
var ans string
37+
for len(loop) > 0 {
38+
cur := loop[0]
39+
loop = loop[1:]
40+
if len(cur) > len(ans) {
41+
ans = cur
42+
}
43+
// 开始做组合
44+
for _, p := range greaterK {
45+
pattern := cur + p
46+
if match(pattern) {
47+
// 这个匹配后,我们需要判断是否还可以继续与其他的z,y x这种组合
48+
loop = append(loop, pattern)
49+
}
50+
}
51+
}
52+
53+
return ans
554
}

leetcode/2001-2100/2014.Longest-Subsequence-Repeated-k-Times/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
k int
15+
expect string
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "letsleetcode", 2, "let"},
18+
{"TestCase2", "bb", 2, "b"},
19+
{"TestCase3", "ab", 2, ""},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.inputs, c.k)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.inputs, c.k)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)