-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1051 from 0xff-dev/2825
Add solution and test-cases for problem 2825
- Loading branch information
Showing
3 changed files
with
69 additions
and
11 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
...code/2801-2900/2825.Make-String-a-Subsequence-Using-Cyclic-Increments/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
15 changes: 13 additions & 2 deletions
15
leetcode/2801-2900/2825.Make-String-a-Subsequence-Using-Cyclic-Increments/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters