forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0028-find-the-index-of-the-first-occurrence-in-a-string.kt
179 lines (156 loc) · 4.46 KB
/
0028-find-the-index-of-the-first-occurrence-in-a-string.kt
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* KMP algorithm
*/
class Solution {
fun strStr(haystack: String, needle: String): Int {
if(needle == "") return 0
val lps = IntArray(needle.length)
var prevLPS = 0
var i = 1
while(i < needle.length) {
if(needle[i] == needle[prevLPS]) {
lps[i] = prevLPS + 1
prevLPS++
i++
}else if(prevLPS == 0) {
lps[i] = 0
i++
}else{
prevLPS = lps[prevLPS - 1]
}
}
i = 0
var j = 0
while (i < haystack.length) {
if(haystack[i] == needle[j]){
i++
j++
}else if(j == 0){
i++
}else{
j = lps[j - 1]
}
if(j == needle.length) {
return i - needle.length
}
}
return -1
}
}
/*
* Rabin-Karp string hashing with bad hash
*/
class Solution {
fun strStr(haystack: String, needle: String): Int {
if(needle.length > haystack.length) return -1
var needleHash = 0
var hayHash = 0
for(i in 0..needle.lastIndex) {
needleHash += needle[i] - 'a'
hayHash += haystack[i] - 'a'
}
for(i in 0..(haystack.length - needle.length)) {
if(hayHash == needleHash) {
for(j in 0..needle.lastIndex) {
if(haystack[i + j] != needle[j])
break
if(j == needle.lastIndex)
return i
}
}
if(i == haystack.length - needle.length)
break
hayHash -= haystack[i] - 'a'
hayHash += haystack[i + needle.length] - 'a'
}
return -1
}
}
/*
* Rabin-karp with proper hash. q should ideally be chosen as an high prime number to avoid no. of collisions.
*/
class Solution {
fun strStr(haystack: String, needle: String): Int {
if(needle.length > haystack.length) return -1
val q = 101
val d = 256
var needleHash = 0
var hayHash = 0
var hash = 1
for (i in 0..needle.lastIndex)
hash = (hash * d) % q
for(i in 0..needle.lastIndex) {
needleHash = (d * needleHash + (needle[i] - 'a')) % q
hayHash = (d * hayHash + (haystack[i] - 'a')) % q
}
for(i in 0..(haystack.length - needle.length)) {
if(hayHash == needleHash) {
for(j in 0..needle.lastIndex) {
if(haystack[i + j] != needle[j])
break
if(j == needle.lastIndex)
return i
}
}
if(i == haystack.length - needle.length)
break
hayHash = (d * hayHash - ((haystack[i] - 'a') * hash) + (haystack[i + needle.length] - 'a')) % q
if(hayHash < 0)
hayHash += q
}
return -1
}
}
/*
* Using Trie to match pattern
*/
class TrieNode() {
val child = arrayOfNulls<TrieNode>(26)
var isEnd = false
}
class Solution {
fun strStr(haystack: String, needle: String): Int {
if(needle == "") return 0
var root: TrieNode? = TrieNode()
var current = root
for(c in needle){
if(current?.child?.get(c - 'a') == null)
current?.child?.set(c - 'a', TrieNode())
current = current?.child?.get(c - 'a')
}
current?.isEnd = true
var i = 0
while(i < haystack.length + 1 - needle.length) {
current = root?.child?.get(haystack[i] - 'a')
var j = i
while(current != null) {
if(current.isEnd == true)
return i
j++
if(j - i < needle.length)
current = current?.child?.get(haystack[j] - 'a')
else
break
}
i++
}
return -1
}
}
/*
* Brute force
*/
class Solution {
fun strStr(haystack: String, needle: String): Int {
if(needle == "") return 0
for(i in 0..(haystack.length - needle.length)) {
for(j in 0..needle.lastIndex) {
if(haystack[i + j] != needle[j])
break
if(j == needle.lastIndex)
return i
}
}
return -1
}
}