Skip to content

Commit 6e1c2ec

Browse files
committed
add more languages
1 parent 0e4c4d9 commit 6e1c2ec

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

dsa/Advance/RabinKarp_README.md

+133
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,139 @@ int main() {
101101
}
102102
```
103103
</TabItem>
104+
<TabItem value="Python" label="Python">
105+
```Python showLineNumbers
106+
def search(pat, txt, q):
107+
d = 256
108+
M = len(pat)
109+
N = len(txt)
110+
p = 0
111+
t = 0
112+
h = 1
113+
114+
for i in range(M - 1):
115+
h = (h * d) % q
116+
117+
for i in range(M):
118+
p = (d * p + ord(pat[i])) % q
119+
t = (d * t + ord(txt[i])) % q
120+
121+
for i in range(N - M + 1):
122+
if p == t:
123+
for j in range(M):
124+
if txt[i + j] != pat[j]:
125+
break
126+
if j == M - 1:
127+
print("Pattern found at index", i)
128+
if i < N - M:
129+
t = (d * (t - ord(txt[i]) * h) + ord(txt[i + M])) % q
130+
if t < 0:
131+
t = t + q
132+
133+
txt = "GEEKS FOR GEEKS"
134+
pat = "GEEK"
135+
q = 101
136+
search(pat, txt, q)
137+
138+
139+
```
140+
</TabItem>
141+
<TabItem value="Java" label="Java">
142+
``` jsx showLineNumbers
143+
import java.util.*;
144+
145+
public class RabinKarp {
146+
public final static int d = 256;
147+
148+
static void search(String pat, String txt, int q) {
149+
int M = pat.length();
150+
int N = txt.length();
151+
int i, j;
152+
int p = 0; // hash value for pattern
153+
int t = 0; // hash value for txt
154+
int h = 1;
155+
156+
for (i = 0; i < M - 1; i++)
157+
h = (h * d) % q;
158+
159+
for (i = 0; i < M; i++) {
160+
p = (d * p + pat.charAt(i)) % q;
161+
t = (d * t + txt.charAt(i)) % q;
162+
}
163+
164+
for (i = 0; i <= N - M; i++) {
165+
if (p == t) {
166+
for (j = 0; j < M; j++) {
167+
if (txt.charAt(i + j) != pat.charAt(j))
168+
break;
169+
}
170+
if (j == M)
171+
System.out.println("Pattern found at index " + i);
172+
}
173+
if (i < N - M) {
174+
t = (d * (t - txt.charAt(i) * h) + txt.charAt(i + M)) % q;
175+
if (t < 0)
176+
t = (t + q);
177+
}
178+
}
179+
}
180+
181+
public static void main(String[] args) {
182+
String txt = "GEEKS FOR GEEKS";
183+
String pat = "GEEK";
184+
int q = 101;
185+
search(pat, txt, q);
186+
}
187+
}
188+
189+
```
190+
</TabItem>
191+
192+
<TabItem value="JavaScript" label="JavaScript">
193+
``` jsx showLineNumbers
194+
function search(pat, txt, q) {
195+
const d = 256;
196+
const M = pat.length;
197+
const N = txt.length;
198+
let p = 0;
199+
let t = 0;
200+
let h = 1;
201+
202+
for (let i = 0; i < M - 1; i++)
203+
h = (h * d) % q;
204+
205+
for (let i = 0; i < M; i++) {
206+
p = (d * p + pat.charCodeAt(i)) % q;
207+
t = (d * t + txt.charCodeAt(i)) % q;
208+
}
209+
210+
for (let i = 0; i <= N - M; i++) {
211+
if (p == t) {
212+
let j;
213+
for (j = 0; j < M; j++) {
214+
if (txt.charAt(i + j) != pat.charAt(j))
215+
break;
216+
}
217+
if (j == M)
218+
console.log("Pattern found at index " + i);
219+
}
220+
if (i < N - M) {
221+
t = (d * (t - txt.charCodeAt(i) * h) + txt.charCodeAt(i + M)) % q;
222+
if (t < 0)
223+
t = (t + q);
224+
}
225+
}
226+
}
227+
228+
const txt = "GEEKS FOR GEEKS";
229+
const pat = "GEEK";
230+
const q = 101;
231+
search(pat, txt, q);
232+
233+
```
234+
</TabItem>
235+
236+
104237
</Tabs>
105238

106239
### Complexity Analysis

0 commit comments

Comments
 (0)