-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
127 lines (113 loc) · 2.46 KB
/
main.go
File metadata and controls
127 lines (113 loc) · 2.46 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
115
116
117
118
119
120
121
122
123
124
125
126
127
// Source: https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string
// Title: Find the Index of the First Occurrence in a String
// Difficulty: Easy
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given two strings `needle` and `haystack`, return the index of the first occurrence of `needle` in `haystack`, or `-1` if `needle` is not part of `haystack`.
//
// **Example 1:**
//
// ```
// Input: haystack = "sadbutsad", needle = "sad"
// Output: 0
// Explanation: "sad" occurs at index 0 and 6.
// The first occurrence is at index 0, so we return 0.
// ```
//
// **Example 2:**
//
// ```
// Input: haystack = "leetcode", needle = "leeto"
// Output: -1
// Explanation: "leeto" did not occur in "leetcode", so we return -1.
// ```
//
// **Constraints:**
//
// - `1 <= haystack.length, needle.length <= 10^4`
// - `haystack` and `needle` consist of only lowercase English characters.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
import "strings"
func strStr(text string, pattern string) int {
return strings.Index(text, pattern)
}
// Naive
func strStr2(text string, pattern string) int {
n, m := len(text), len(pattern)
if n < m {
return -1
}
for i := range n - m + 1 {
if text[i:i+m] == pattern {
return i
}
}
return -1
}
// Rabin-Karp Algorithm
//
// Use char sum as hash
func strStr3(text string, pattern string) int {
n, m := len(text), len(pattern)
if n < m {
return -1
}
needleHash := 0
for i := range m {
needleHash += int(pattern[i])
}
windowHash := 0
for i := range m - 1 {
windowHash += int(text[i])
}
for i := range n - m + 1 {
windowHash += int(text[i+m-1])
if windowHash == needleHash && text[i:i+m] == pattern {
return i
}
windowHash -= int(text[i])
}
return -1
}
// KMP Algorithm
func strStr4(text string, pattern string) int {
n, m := len(text), len(pattern)
if n < m {
return -1
}
// Build KPS
lps := make([]int, m)
{
i, j := 1, 0
for i < m {
if pattern[i] == pattern[j] {
j++
lps[i] = j
i++
} else if j > 0 {
j = lps[j-1]
} else {
lps[i] = 0
i++
}
}
}
// Find substring
i, j := 0, 0
for i < n {
if text[i] == pattern[j] {
i++
j++
if j == m {
return i - m
}
} else if j > 0 {
j = lps[j-1]
} else {
i++
}
}
return -1
}