File tree 3 files changed +113
-0
lines changed
main/java/com/packt/datastructuresandalg/lesson5/activity/badcharacterrule
test/java/com/packt/datastructuresandalg/lesson5/activity/badcharacterrule
3 files changed +113
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .packt .datastructuresandalg .lesson5 .activity .badcharacterrule ;
2
+
3
+ import java .util .ArrayList ;
4
+ import java .util .List ;
5
+
6
+ public class BadCharacterRule {
7
+ public List <Integer > match (String P , String T ) {
8
+ int n = T .length ();
9
+ int m = P .length ();
10
+
11
+ int e = 256 ;
12
+ int right [][] = new int [m ][e ];
13
+ // Populate right[][] with the correct values
14
+
15
+ List <Integer > shifts = new ArrayList <>();
16
+ int skip ;
17
+ for (int i = 0 ; i < n - m + 1 ; i += skip ) {
18
+ skip = 0 ;
19
+ for (int j = m - 1 ; j >= 0 ; j --) {
20
+ if (P .charAt (j ) != T .charAt (i + j )) {
21
+ skip = Math .max (1 , j - right [j ][T .charAt (i + j )]);
22
+ break ;
23
+ }
24
+ }
25
+ if (skip == 0 ) {
26
+ shifts .add (i );
27
+ skip = 1 ;
28
+ }
29
+ }
30
+ return shifts ;
31
+ }
32
+ }
Original file line number Diff line number Diff line change
1
+ package com .packt .datastructuresandalg .lesson5 .activity .badcharacterrule .solution ;
2
+
3
+ import java .util .ArrayList ;
4
+ import java .util .List ;
5
+
6
+ public class BadCharacterRule {
7
+ public List <Integer > match (String P , String T ) {
8
+ int n = T .length ();
9
+ int m = P .length ();
10
+
11
+ int e = 256 ;
12
+ int right [][] = new int [m ][e ];
13
+ for (int i = 0 ; i < m ; i ++)
14
+ for (int j = 0 ; j < e ; j ++)
15
+ right [i ][j ] = -1 ;
16
+ for (int i = 0 ; i < m ; i ++) {
17
+ if (i != 0 )
18
+ for (int j = 0 ; j < e ; j ++)
19
+ right [i ][j ] = right [i - 1 ][j ];
20
+ right [i ][P .charAt (i )] = i ;
21
+ }
22
+
23
+ List <Integer > shifts = new ArrayList <>();
24
+ int skip ;
25
+ for (int i = 0 ; i < n - m + 1 ; i += skip ) {
26
+ skip = 0 ;
27
+ for (int j = m - 1 ; j >= 0 ; j --) {
28
+ if (P .charAt (j ) != T .charAt (i + j )) {
29
+ skip = Math .max (1 , j - right [j ][T .charAt (i + j )]);
30
+ break ;
31
+ }
32
+ }
33
+ if (skip == 0 ) {
34
+ shifts .add (i );
35
+ skip = 1 ;
36
+ }
37
+ }
38
+ return shifts ;
39
+ }
40
+
41
+ public static void main (String [] args ) {
42
+ BadCharacterRule bcr = new BadCharacterRule ();
43
+ List <Integer > matches = bcr .match ("rabrabracad" , "abacadabrabracabracadabrabrabracad" );
44
+ for (Integer i : matches )
45
+ System .out .println (i );
46
+ }
47
+ }
Original file line number Diff line number Diff line change
1
+ package com .packt .datastructuresandalg .lesson5 .activity .badcharacterrule ;
2
+
3
+ import junit .framework .TestCase ;
4
+
5
+ import java .util .ArrayList ;
6
+ import java .util .Arrays ;
7
+
8
+ public class BadCharacterRuleTest extends TestCase {
9
+ BadCharacterRule bcr = new BadCharacterRule ();
10
+
11
+ public void test1 () {
12
+ assertTrue (bcr .match ("abc" , "abc" ).equals (Arrays .asList (0 )));
13
+ }
14
+
15
+ public void test2 () {
16
+ assertTrue (bcr .match ("abd" , "abc" ).equals (new ArrayList <>()));
17
+ }
18
+
19
+ public void test3 () {
20
+ assertTrue (bcr .match ("abcde" , "abc" ).equals (new ArrayList <>()));
21
+ }
22
+
23
+ public void test4 () {
24
+ assertTrue (bcr .match ("aab" , "acaabc" ).equals (Arrays .asList (2 )));
25
+ }
26
+
27
+ public void test5 () {
28
+ assertTrue (bcr .match ("aab" , "acaabcaab" ).equals (Arrays .asList (2 , 6 )));
29
+ }
30
+
31
+ public void test6 () {
32
+ assertTrue (bcr .match ("rabrabracad" , "abacadabrabracabracadabrabrabracad" ).equals (Arrays .asList (23 )));
33
+ }
34
+ }
You can’t perform that action at this time.
0 commit comments