-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3008-FindBeautifulIndicesInTheGivenArrayII.go
More file actions
114 lines (104 loc) · 3.97 KB
/
3008-FindBeautifulIndicesInTheGivenArrayII.go
File metadata and controls
114 lines (104 loc) · 3.97 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
// 3008. Find Beautiful Indices in the Given Array II
// You are given a 0-indexed string s, a string a, a string b, and an integer k.
// An index i is beautiful if:
// 0 <= i <= s.length - a.length
// s[i..(i + a.length - 1)] == a
// There exists an index j such that:
// 0 <= j <= s.length - b.length
// s[j..(j + b.length - 1)] == b
// |j - i| <= k
// Return the array that contains beautiful indices in sorted order from smallest to largest.
// Example 1:
// Input: s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
// Output: [16,33]
// Explanation: There are 2 beautiful indices: [16,33].
// - The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
// - The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
// Thus we return [16,33] as the result.
// Example 2:
// Input: s = "abcd", a = "a", b = "a", k = 4
// Output: [0]
// Explanation: There is 1 beautiful index: [0].
// - The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
// Thus we return [0] as the result.
// Constraints:
// 1 <= k <= s.length <= 5 * 10^5
// 1 <= a.length, b.length <= 5 * 10^5
// s, a, and b contain only lowercase English letters.
import "fmt"
func beautifulIndices(s string, a string, b string, k int) []int {
sl := len(s)
pat := func(pattern string) []int {
n, l, i := len(pattern), 0 , 1
res := make([]int, n)
res[0] = 0
for i < n {
if pattern[i] == pattern[l] {
l++
res[i] = l
i++
} else {
if l != 0 {
l = res[l-1]
} else {
res[i] = l
i++
}
}
}
return res
}
kmp := func(pat string, lps []int) []int {
res, i, j, n := []int{}, 0, 0, len(pat)
for sl - i >= n - j {
if s[i] == pat[j] {
i++
j++
}
if j == n {
res = append(res, i - n)
j = lps[j - 1]
} else if s[i] != pat[j] {
if j!=0 {
j = lps[j - 1]
} else {
i++
}
}
}
return res
}
res, indexA, indexB := []int{}, kmp(a, pat(a)), kmp(b, pat(b))
i, j := 0, 0
for i < len(indexA) && j < len(indexB) {
if indexA[i] + k >= indexB[j] && indexA[i] - k <= indexB[j] {
res = append(res, indexA[i])
i++
} else if indexA[i] - k > indexB[j] {
j++
} else {
i++
}
}
return res
}
func main() {
// Example 1:
// Input: s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
// Output: [16,33]
// Explanation: There are 2 beautiful indices: [16,33].
// - The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
// - The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
// Thus we return [16,33] as the result.
fmt.Println(beautifulIndices("isawsquirrelnearmysquirrelhouseohmy", "my" ,"squirrel", 15)) // [16,33]
// Example 2:
// Input: s = "abcd", a = "a", b = "a", k = 4
// Output: [0]
// Explanation: There is 1 beautiful index: [0].
// - The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
// Thus we return [0] as the result.
fmt.Println(beautifulIndices("abcd", "a" ,"a", 4)) // [0]
// fmt.Println(beautifulIndices1("isawsquirrelnearmysquirrelhouseohmy", "my" ,"squirrel", 15)) // [16,33]
// fmt.Println(beautifulIndices1("abcd", "a" ,"a", 4)) // [0]
}