-
Notifications
You must be signed in to change notification settings - Fork 2
/
StringPatternMatching.java
46 lines (41 loc) · 1014 Bytes
/
StringPatternMatching.java
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
package algo;
import org.junit.Assert;
import org.junit.Test;
public class StringPatternMatching
{
@Test
public void testPattern()
{
Assert.assertEquals(9,
findPattern("ciao","012345678ciao012345sadfadsfadsfdsf"));
Assert.assertEquals(0,
findPattern("ciao","ciao012345678ciao012345sadfadsfadsfdsf"));
Assert.assertEquals(9,
findPattern("ciao","012345678ciao"));
Assert.assertEquals(9,
findPattern("ciao","cia345678ciao012345sadfadsfadsfdsf"));
Assert.assertEquals(-1,
findPattern("ciao","01234567dfadsfadsfdsf"));
}
/**
* solution 2.5.3
*
* @param pattern
* @param text
* @return
*/
private int findPattern(String pattern, String text)
{
for(int i = 0; i<= (text.length() - pattern.length()); i++)
{
int j=0;
while(((j< pattern.length()) && (text.charAt(i + j) ==pattern.charAt(j))))
{
j++;
}
if(j== pattern.length())
return i;
}
return -1;
}
}