Skip to content

Commit f3dc0e5

Browse files
committed
Add solution and test-cases for problem 2370
1 parent 09a1dcb commit f3dc0e5

File tree

3 files changed

+62
-26
lines changed

3 files changed

+62
-26
lines changed

leetcode/2301-2400/2370.Longest-Ideal-Subsequence/README.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# [2370.Longest Ideal Subsequence][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` consisting of lowercase letters and an integer `k`. We call a string `t` **ideal** if the following conditions are satisfied:
5+
6+
- `t` is a **subsequence** of the string `s`.
7+
- The absolute difference in the alphabet order of every two **adjacent** letters in `t` is less than or equal to `k`.
8+
9+
Return the length of the **longest** ideal string.
10+
11+
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.
12+
13+
**Note** that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of `'a'` and `'z'` is `25`, not `1`.
714

815
**Example 1:**
916

1017
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
18+
Input: s = "acfgbd", k = 2
19+
Output: 4
20+
Explanation: The longest ideal string is "acbd". The length of this string is 4, so 4 is returned.
21+
Note that "acfgbd" is not ideal because 'c' and 'f' have a difference of 3 in alphabet order.
1322
```
1423

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

20-
### 思路1
21-
> ...
22-
Longest Ideal Subsequence
23-
```go
2426
```
25-
27+
Input: s = "abcd", k = 3
28+
Output: 4
29+
Explanation: The longest ideal string is "abcd". The length of this string is 4, so 4 is returned.
30+
```
2631

2732
## 结语
2833

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string, k int) int {
4+
dis := [26]int{}
5+
dis[s[0]-'a'] = 1
6+
for i := 1; i < len(s); i++ {
7+
td := uint8(0)
8+
for ; td <= uint8(k); td++ {
9+
if next := s[i] + td; next >= 'a' && next <= 'z' {
10+
if dis[next-'a'] != 0 && dis[next-'a']+1 > dis[s[i]-'a'] {
11+
dis[s[i]-'a'] = dis[next-'a'] + 1
12+
}
13+
}
14+
15+
}
16+
td = uint8(1)
17+
for ; td <= uint8(k); td++ {
18+
if pre := s[i] - td; pre >= 'a' && pre <= 'z' {
19+
if dis[pre-'a'] != 0 && dis[pre-'a']+1 > dis[s[i]-'a'] {
20+
dis[s[i]-'a'] = dis[pre-'a'] + 1
21+
}
22+
}
23+
}
24+
if dis[s[i]-'a'] == 0 {
25+
dis[s[i]-'a'] = 1
26+
}
27+
}
28+
ans := 1
29+
for i := 0; i < 26; i++ {
30+
if dis[i] > ans {
31+
ans = dis[i]
32+
}
33+
}
34+
return ans
535
}

leetcode/2301-2400/2370.Longest-Ideal-Subsequence/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 int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "acfgbd", 2, 4},
18+
{"TestCase2", "abcd", 3, 4},
19+
{"TestCase3", "pvjcci", 4, 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)