-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBMA.java
More file actions
70 lines (63 loc) · 1.66 KB
/
BMA.java
File metadata and controls
70 lines (63 loc) · 1.66 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
package Algorithms;
public class BMA
{
/*public static void main(String[] args)
{
//(BoyerMoore("Is the pattern a substring?", "substr"));
}*/
public void BoyerMoore(String T, String P)
{
int flag=0,pos=0;
int i = P.length() -1;
int j = P.length() -1;
do
{
if (P.charAt(j) == T.charAt(i))
{
if (j == 0)
{
pos=i;
flag=1;// pattern found
System.out.println("Pattern found at position: "+ pos);
break;
}
else
{
i--;
j--;
}
}
else
{
i = i + P.length() - min(j, 1+last(T.charAt(i), P));
j = P.length()-1;
}
} while(i <= T.length()-1);
if(flag==1)
System.out.println("Pattern found at position: "+ pos);
else
System.out.println("Pattern not found");
}
// Returns index of last occurrence of character in pattern.
public static int last(char c, String P)
{
for (int i=P.length()-1; i>=0; i--)
{
if (P.charAt(i) == c)
{
return i;
}
}
return -1;
}
// Returns the minimum of two integers.
public static int min(int a, int b)
{
if (a < b)
return a;
else if (b < a)
return b;
else
return a;
}
}