File tree 2 files changed +6
-2
lines changed
src/algorithms/string/knuth-morris-pratt
2 files changed +6
-2
lines changed Original file line number Diff line number Diff line change @@ -2,8 +2,8 @@ import knuthMorrisPratt from '../knuthMorrisPratt';
2
2
3
3
describe ( 'knuthMorrisPratt' , ( ) => {
4
4
it ( 'should find word position in given text' , ( ) => {
5
- expect ( knuthMorrisPratt ( '' , '' ) ) . toBe ( - 1 ) ;
6
- expect ( knuthMorrisPratt ( 'a' , '' ) ) . toBe ( - 1 ) ;
5
+ expect ( knuthMorrisPratt ( '' , '' ) ) . toBe ( 0 ) ;
6
+ expect ( knuthMorrisPratt ( 'a' , '' ) ) . toBe ( 0 ) ;
7
7
expect ( knuthMorrisPratt ( 'a' , 'a' ) ) . toBe ( 0 ) ;
8
8
expect ( knuthMorrisPratt ( 'abcbcglx' , 'abca' ) ) . toBe ( - 1 ) ;
9
9
expect ( knuthMorrisPratt ( 'abcbcglx' , 'bcgl' ) ) . toBe ( 3 ) ;
Original file line number Diff line number Diff line change @@ -30,6 +30,10 @@ function buildPatternTable(word) {
30
30
* @return {number }
31
31
*/
32
32
export default function knuthMorrisPratt ( text , word ) {
33
+ if ( word . length === 0 ) {
34
+ return 0 ;
35
+ }
36
+
33
37
let textIndex = 0 ;
34
38
let wordIndex = 0 ;
35
39
You can’t perform that action at this time.
0 commit comments