public int search ( String text ) { // Searching int i = pattern . length ( ) - 1 ; int j = pattern . length ( ) - 1 ; while ( i < text . length ( ) ) { if ( pattern . charAt ( j ) == text . charAt ( i ) ) { if ( j == 0 ) { //the left-most match is found return i ; } j -- ; i -- ; } else { //a difference i += pattern . length ( ) - j - 1 + Math . max ( j - last [ text . charAt ( i ) ] , match [ j ] ) ; j = pattern . length ( ) - 1 ; } } return - 1 ; }
Searching the pattern in the text