Skip to content

Commit 0361fe5

Browse files
dubzzztrekhleb
authored andcommitted
Fix knuthMorrisPratt for empty word request (trekhleb#101)
1 parent 76461f2 commit 0361fe5

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/algorithms/string/knuth-morris-pratt/__test__/knuthMorrisPratt.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import knuthMorrisPratt from '../knuthMorrisPratt';
22

33
describe('knuthMorrisPratt', () => {
44
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);
77
expect(knuthMorrisPratt('a', 'a')).toBe(0);
88
expect(knuthMorrisPratt('abcbcglx', 'abca')).toBe(-1);
99
expect(knuthMorrisPratt('abcbcglx', 'bcgl')).toBe(3);

src/algorithms/string/knuth-morris-pratt/knuthMorrisPratt.js

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ function buildPatternTable(word) {
3030
* @return {number}
3131
*/
3232
export default function knuthMorrisPratt(text, word) {
33+
if (word.length === 0) {
34+
return 0;
35+
}
36+
3337
let textIndex = 0;
3438
let wordIndex = 0;
3539

0 commit comments

Comments
 (0)