From 01c3d750d20e86c7aff7d0df8b017f82bef9b3de Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 4 Dec 2024 09:26:21 +0800 Subject: [PATCH] Add solution and test-cases for problem 2825 --- .../README.md | 47 +++++++++++++++++++ .../Solution.go | 15 +++++- .../Solution_test.go | 18 +++---- 3 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 leetcode/2801-2900/2825.Make-String-a-Subsequence-Using-Cyclic-Increments/README.md diff --git a/leetcode/2801-2900/2825.Make-String-a-Subsequence-Using-Cyclic-Increments/README.md b/leetcode/2801-2900/2825.Make-String-a-Subsequence-Using-Cyclic-Increments/README.md new file mode 100644 index 000000000..46397a55b --- /dev/null +++ b/leetcode/2801-2900/2825.Make-String-a-Subsequence-Using-Cyclic-Increments/README.md @@ -0,0 +1,47 @@ +# [2825.Make String a Subsequence Using Cyclic Increments][title] + +## Description +You are given two **0-indexed** strings `str1` and `str2`. + +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'`. + +Return `true` if it is possible to make `str2` a subsequence of `str1` by performing the operation **at most once**, and `false` otherwise. + +**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. + +**Example 1:** + +``` +Input: str1 = "abc", str2 = "ad" +Output: true +Explanation: Select index 2 in str1. +Increment str1[2] to become 'd'. +Hence, str1 becomes "abd" and str2 is now a subsequence. Therefore, true is returned. +``` + +**Example 2:** + +``` +Input: str1 = "zc", str2 = "ad" +Output: true +Explanation: Select indices 0 and 1 in str1. +Increment str1[0] to become 'a'. +Increment str1[1] to become 'd'. +Hence, str1 becomes "ad" and str2 is now a subsequence. Therefore, true is returned. +``` + +**Example 3:** + +``` +Input: str1 = "ab", str2 = "d" +Output: false +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. +Therefore, false is returned. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/make-string-a-subsequence-using-cyclic-increments +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2801-2900/2825.Make-String-a-Subsequence-Using-Cyclic-Increments/Solution.go b/leetcode/2801-2900/2825.Make-String-a-Subsequence-Using-Cyclic-Increments/Solution.go index d115ccf5e..f72c9780b 100755 --- a/leetcode/2801-2900/2825.Make-String-a-Subsequence-Using-Cyclic-Increments/Solution.go +++ b/leetcode/2801-2900/2825.Make-String-a-Subsequence-Using-Cyclic-Increments/Solution.go @@ -1,5 +1,16 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(str1 string, str2 string) bool { + if len(str2) > len(str1) { + return false + } + b := 0 + for i := 0; i < len(str1) && b < len(str2); i++ { + t1 := str1[i] - 'a' + t2 := str2[b] - 'a' + if t1 == t2 || t2 == (t1+1+26)%26 { + b++ + } + } + return b == len(str2) } diff --git a/leetcode/2801-2900/2825.Make-String-a-Subsequence-Using-Cyclic-Increments/Solution_test.go b/leetcode/2801-2900/2825.Make-String-a-Subsequence-Using-Cyclic-Increments/Solution_test.go index 14ff50eb4..01a9ad107 100755 --- a/leetcode/2801-2900/2825.Make-String-a-Subsequence-Using-Cyclic-Increments/Solution_test.go +++ b/leetcode/2801-2900/2825.Make-String-a-Subsequence-Using-Cyclic-Increments/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool + s1, s2 string expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "abc", "ad", true}, + {"TestCase2", "zc", "ad", true}, + {"TestCase3", "ab", "d", false}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.s1, c.s2) 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.s1, c.s2) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }