-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2825-MakeStringASubsequenceUsingCyclicIncrements.go
More file actions
97 lines (84 loc) · 3.42 KB
/
2825-MakeStringASubsequenceUsingCyclicIncrements.go
File metadata and controls
97 lines (84 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
// 2825. Make String a Subsequence Using Cyclic Increments
// 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.
// Constraints:
// 1 <= str1.length <= 10^5
// 1 <= str2.length <= 10^5
// str1 and str2 consist of only lowercase English letters.
import "fmt"
func canMakeSubsequence(str1 string, str2 string) bool {
i, n := 0, len(str2)
for _, v := range str1 {
d := byte('a')
if v != 'z' {
d = byte(v + 1)
}
if i < n && (str2[i] == byte(v) || str2[i] == d) {
i++
}
}
return i == n
}
// 双指针
func canMakeSubsequence1(str1 string, str2 string) bool {
if len(str1) < len(str2) { return false }
i, j := 0, 0
for i < len(str1) && j < len(str2) {
if str1[i] == str2[j] || (str1[i] + 1 == str2[j]) || (str1[i] == 'z' && str2[j] == 'a') {
i++
j++
} else if str1[i] + 1 != str2[j] {
i++
}
}
return j == len(str2)
}
func main() {
// 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.
fmt.Println(canMakeSubsequence("abc", "ad")) // true
// 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.
fmt.Println(canMakeSubsequence("zc", "ad")) // true
// 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.
fmt.Println(canMakeSubsequence("ab", "d")) // false
fmt.Println(canMakeSubsequence1("abc", "ad")) // true
fmt.Println(canMakeSubsequence1("zc", "ad")) // true
fmt.Println(canMakeSubsequence1("ab", "d")) // false
}