Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 19a6b3a

Browse files
committedJul 20, 2018
Fix knuthMorrisPratt for empty word request
1 parent fc89336 commit 19a6b3a

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed
 

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

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

33
describe('knuthMorrisPratt', () => {
44
it('should find word position in given text', () => {
5+
expect(knuthMorrisPratt('', '')).toBe(0);
6+
expect(knuthMorrisPratt('a', '')).toBe(0);
7+
expect(knuthMorrisPratt('a', 'a')).toBe(0);
58
expect(knuthMorrisPratt('abcbcglx', 'abca')).toBe(-1);
69
expect(knuthMorrisPratt('abcbcglx', 'bcgl')).toBe(3);
710
expect(knuthMorrisPratt('abcxabcdabxabcdabcdabcy', 'abcdabcy')).toBe(15);

‎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)
Please sign in to comment.