Skip to content

Commit 085cef8

Browse files
ante003pivovarit
authored andcommitted
string search algorithms article (eugenp#2460)
* - added code for string search algorithms article - added tests * - fixed algorithm for finding prime numbers
1 parent 4c7f165 commit 085cef8

2 files changed

Lines changed: 219 additions & 0 deletions

File tree

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
package com.baeldung.algorithms.string.search;
2+
3+
import java.math.BigInteger;
4+
import java.util.Random;
5+
6+
public class StringSearchAlgorithms {
7+
public static long getBiggerPrime(int m) {
8+
BigInteger prime = BigInteger.probablePrime(getNumberOfBits(m) + 1, new Random());
9+
return prime.longValue();
10+
}
11+
12+
public static long getLowerPrime(long number) {
13+
BigInteger prime = BigInteger.probablePrime(getNumberOfBits(number) - 1, new Random());
14+
return prime.longValue();
15+
}
16+
17+
private static int getNumberOfBits(final int number) {
18+
return Integer.SIZE - Integer.numberOfLeadingZeros(number);
19+
}
20+
21+
private static int getNumberOfBits(final long number) {
22+
return Long.SIZE - Long.numberOfLeadingZeros(number);
23+
}
24+
25+
public static int simpleTextSearch(char[] pattern, char[] text) {
26+
int patternSize = pattern.length;
27+
int textSize = text.length;
28+
29+
int i = 0;
30+
31+
while ((i + patternSize) <= textSize) {
32+
int j = 0;
33+
while (text[i + j] == pattern[j]) {
34+
j += 1;
35+
if (j >= patternSize)
36+
return i;
37+
}
38+
i += 1;
39+
}
40+
41+
return -1;
42+
}
43+
44+
public static int RabinKarpMethod(char[] pattern, char[] text) {
45+
int patternSize = pattern.length; // m
46+
int textSize = text.length; // n
47+
48+
long prime = getBiggerPrime(patternSize);
49+
50+
long r = 1;
51+
for (int i = 0; i < patternSize - 1; i++) {
52+
r *= 2;
53+
r = r % prime;
54+
}
55+
56+
long[] t = new long[textSize];
57+
t[0] = 0;
58+
59+
long pfinger = 0;
60+
61+
for (int j = 0; j < patternSize; j++) {
62+
t[0] = (2 * t[0] + text[j]) % prime;
63+
pfinger = (2 * pfinger + pattern[j]) % prime;
64+
}
65+
66+
int i = 0;
67+
boolean passed = false;
68+
69+
int diff = textSize - patternSize;
70+
for (i = 0; i <= diff; i++) {
71+
if (t[i] == pfinger) {
72+
passed = true;
73+
for (int k = 0; k < patternSize; k++) {
74+
if (text[i + k] != pattern[k]) {
75+
passed = false;
76+
break;
77+
}
78+
}
79+
80+
if (passed) {
81+
return i;
82+
}
83+
}
84+
85+
if (i < diff) {
86+
long value = 2 * (t[i] - r * text[i]) + text[i + patternSize];
87+
t[i + 1] = ((value % prime) + prime) % prime;
88+
}
89+
}
90+
return -1;
91+
92+
}
93+
94+
public static int KnuthMorrisPrattSearch(char[] pattern, char[] text) {
95+
int patternSize = pattern.length; // m
96+
int textSize = text.length; // n
97+
98+
int i = 0, j = 0;
99+
100+
int[] shift = KnuthMorrisPrattShift(pattern);
101+
102+
while ((i + patternSize) <= textSize) {
103+
while (text[i + j] == pattern[j]) {
104+
j += 1;
105+
if (j >= patternSize)
106+
return i;
107+
}
108+
109+
if (j > 0) {
110+
i += shift[j - 1];
111+
j = Math.max(j - shift[j - 1], 0);
112+
} else {
113+
i++;
114+
j = 0;
115+
}
116+
}
117+
return -1;
118+
}
119+
120+
public static int[] KnuthMorrisPrattShift(char[] pattern) {
121+
int patternSize = pattern.length;
122+
123+
int[] shift = new int[patternSize];
124+
shift[0] = 1;
125+
126+
int i = 1, j = 0;
127+
128+
while ((i + j) < patternSize) {
129+
if (pattern[i + j] == pattern[j]) {
130+
shift[i + j] = i;
131+
j++;
132+
} else {
133+
if (j == 0)
134+
shift[i] = i + 1;
135+
136+
if (j > 0) {
137+
i = i + shift[j - 1];
138+
j = Math.max(j - shift[j - 1], 0);
139+
} else {
140+
i = i + 1;
141+
j = 0;
142+
}
143+
}
144+
}
145+
return shift;
146+
}
147+
148+
public static int BoyerMooreHorspoolSimpleSearch(char[] pattern, char[] text) {
149+
int patternSize = pattern.length;
150+
int textSize = text.length;
151+
152+
int i = 0, j = 0;
153+
154+
while ((i + patternSize) <= textSize) {
155+
j = patternSize - 1;
156+
while (text[i + j] == pattern[j]) {
157+
j--;
158+
if (j < 0)
159+
return i;
160+
}
161+
i++;
162+
}
163+
return -1;
164+
}
165+
166+
public static int BoyerMooreHorspoolSearch(char[] pattern, char[] text) {
167+
168+
int shift[] = new int[256];
169+
170+
for (int k = 0; k < 256; k++) {
171+
shift[k] = pattern.length;
172+
}
173+
174+
for (int k = 0; k < pattern.length - 1; k++) {
175+
shift[pattern[k]] = pattern.length - 1 - k;
176+
}
177+
178+
int i = 0, j = 0;
179+
180+
while ((i + pattern.length) <= text.length) {
181+
j = pattern.length - 1;
182+
183+
while (text[i + j] == pattern[j]) {
184+
j -= 1;
185+
if (j < 0)
186+
return i;
187+
}
188+
189+
i = i + shift[text[i + pattern.length - 1]];
190+
191+
}
192+
return -1;
193+
}
194+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package algorithms;
2+
3+
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
import com.baeldung.algorithms.string.search.StringSearchAlgorithms;
8+
9+
public class StringSearchAlgorithmsTest {
10+
11+
12+
@Test
13+
public void testStringSearchAlgorithms(){
14+
String text = "This is some nice text.";
15+
String pattern = "some";
16+
17+
int realPosition = text.indexOf(pattern);
18+
Assert.assertTrue(realPosition == StringSearchAlgorithms.simpleTextSearch(pattern.toCharArray(), text.toCharArray()));
19+
Assert.assertTrue(realPosition == StringSearchAlgorithms.RabinKarpMethod(pattern.toCharArray(), text.toCharArray()));
20+
Assert.assertTrue(realPosition == StringSearchAlgorithms.KnuthMorrisPrattSearch(pattern.toCharArray(), text.toCharArray()));
21+
Assert.assertTrue(realPosition == StringSearchAlgorithms.BoyerMooreHorspoolSimpleSearch(pattern.toCharArray(), text.toCharArray()));
22+
Assert.assertTrue(realPosition == StringSearchAlgorithms.BoyerMooreHorspoolSearch(pattern.toCharArray(), text.toCharArray()));
23+
}
24+
25+
}

0 commit comments

Comments
 (0)