diff --git a/leetcode/2001-2100/2014.Longest-Subsequence-Repeated-k-Times/1.png b/leetcode/2001-2100/2014.Longest-Subsequence-Repeated-k-Times/1.png new file mode 100644 index 000000000..a68e49478 Binary files /dev/null and b/leetcode/2001-2100/2014.Longest-Subsequence-Repeated-k-Times/1.png differ diff --git a/leetcode/2001-2100/2014.Longest-Subsequence-Repeated-k-Times/README.md b/leetcode/2001-2100/2014.Longest-Subsequence-Repeated-k-Times/README.md index 8a2489525..a60e5c3ac 100755 --- a/leetcode/2001-2100/2014.Longest-Subsequence-Repeated-k-Times/README.md +++ b/leetcode/2001-2100/2014.Longest-Subsequence-Repeated-k-Times/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/2001-2100/2014.Longest-Subsequence-Repeated-k-Times/Solution.go b/leetcode/2001-2100/2014.Longest-Subsequence-Repeated-k-Times/Solution.go index d115ccf5e..651c40f0c 100644 --- a/leetcode/2001-2100/2014.Longest-Subsequence-Repeated-k-Times/Solution.go +++ b/leetcode/2001-2100/2014.Longest-Subsequence-Repeated-k-Times/Solution.go @@ -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 } diff --git a/leetcode/2001-2100/2014.Longest-Subsequence-Repeated-k-Times/Solution_test.go b/leetcode/2001-2100/2014.Longest-Subsequence-Repeated-k-Times/Solution_test.go index 14ff50eb4..11c3f87af 100644 --- a/leetcode/2001-2100/2014.Longest-Subsequence-Repeated-k-Times/Solution_test.go +++ b/leetcode/2001-2100/2014.Longest-Subsequence-Repeated-k-Times/Solution_test.go @@ -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() { }