Skip to content

Add solution and test-cases for problem 2014 #1245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
# [2014.Longest Subsequence Repeated k Times][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description

**Example 1:**
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`.

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.

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.

- 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"`.

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.

**Example 1:**

![1](./1.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "letsleetcode", k = 2
Output: "let"
Explanation: There are two longest subsequences repeated 2 times: "let" and "ete".
"let" is the lexicographically largest one.
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Longest Subsequence Repeated k Times
```go
```
Input: s = "bb", k = 2
Output: "b"
Explanation: The longest subsequence repeated 2 times is "b".
```

**Example 3:**

```
Input: s = "ab", k = 2
Output: ""
Explanation: There is no subsequence repeated 2 times. Empty string is returned.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,54 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(s string, k int) string {
count := [26]int{}
for _, b := range s {
count[b-'a']++
}
var greaterK []string
for i := 25; i >= 0; i-- {
if count[i] >= k {
greaterK = append(greaterK, string('a'+byte(i)))
}
}

var match func(string) bool
match = func(pattern string) bool {
i, c := 0, 0
for j := 0; j < len(s); j++ {
if s[j] == pattern[i] {
i++
if i == len(pattern) {
c++
i = 0
if c >= k {
return true
}
}
}
}
return false
}

loop := make([]string, len(greaterK))
copy(loop, greaterK)
// z, y, x, 我们应该需要判断 z ok (zz) ok , zzz(ok) / 以及 zx, zy ...
var ans string
for len(loop) > 0 {
cur := loop[0]
loop = loop[1:]
if len(cur) > len(ans) {
ans = cur
}
// 开始做组合
for _, p := range greaterK {
pattern := cur + p
if match(pattern) {
// 这个匹配后,我们需要判断是否还可以继续与其他的z,y x这种组合
loop = append(loop, pattern)
}
}
}

return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
k int
expect string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "letsleetcode", 2, "let"},
{"TestCase2", "bb", 2, "b"},
{"TestCase3", "ab", 2, ""},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.k)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.inputs, c.k)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading