Skip to content

Commit 01c3d75

Browse files
committed
Add solution and test-cases for problem 2825
1 parent dbd4dda commit 01c3d75

File tree

3 files changed

+69
-11
lines changed

3 files changed

+69
-11
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# [2825.Make String a Subsequence Using Cyclic Increments][title]
2+
3+
## Description
4+
You are given two **0-indexed** strings `str1` and `str2`.
5+
6+
In an operation, you select a **set** of indices in `str1`, and for each index `i` in the set, increment `str1[i]` to the next character **cyclically**. That is `'a'` becomes `'b'`, `'b'` becomes `'c'`, and so on, and `'z'` becomes `'a'`.
7+
8+
Return `true` if it is possible to make `str2` a subsequence of `str1` by performing the operation **at most once**, and `false` otherwise.
9+
10+
**Note**: A subsequence of a string is a new string that is formed from the original string by deleting some (possibly none) of the characters without disturbing the relative positions of the remaining characters.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: str1 = "abc", str2 = "ad"
16+
Output: true
17+
Explanation: Select index 2 in str1.
18+
Increment str1[2] to become 'd'.
19+
Hence, str1 becomes "abd" and str2 is now a subsequence. Therefore, true is returned.
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: str1 = "zc", str2 = "ad"
26+
Output: true
27+
Explanation: Select indices 0 and 1 in str1.
28+
Increment str1[0] to become 'a'.
29+
Increment str1[1] to become 'd'.
30+
Hence, str1 becomes "ad" and str2 is now a subsequence. Therefore, true is returned.
31+
```
32+
33+
**Example 3:**
34+
35+
```
36+
Input: str1 = "ab", str2 = "d"
37+
Output: false
38+
Explanation: In this example, it can be shown that it is impossible to make str2 a subsequence of str1 using the operation at most once.
39+
Therefore, false is returned.
40+
```
41+
42+
## 结语
43+
44+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
45+
46+
[title]: https://leetcode.com/problems/make-string-a-subsequence-using-cyclic-increments
47+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(str1 string, str2 string) bool {
4+
if len(str2) > len(str1) {
5+
return false
6+
}
7+
b := 0
8+
for i := 0; i < len(str1) && b < len(str2); i++ {
9+
t1 := str1[i] - 'a'
10+
t2 := str2[b] - 'a'
11+
if t1 == t2 || t2 == (t1+1+26)%26 {
12+
b++
13+
}
14+
}
15+
return b == len(str2)
516
}

leetcode/2801-2900/2825.Make-String-a-Subsequence-Using-Cyclic-Increments/Solution_test.go

Lines changed: 9 additions & 9 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
13+
s1, s2 string
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "abc", "ad", true},
17+
{"TestCase2", "zc", "ad", true},
18+
{"TestCase3", "ab", "d", false},
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.s1, c.s2)
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.s1, c.s2)
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)