mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 07:01:18 +08:00
Fix knuthMorrisPratt for empty word request (#101)
This commit is contained in:
parent
76461f29ee
commit
0361fe5cf8
@ -2,8 +2,8 @@ import knuthMorrisPratt from '../knuthMorrisPratt';
|
||||
|
||||
describe('knuthMorrisPratt', () => {
|
||||
it('should find word position in given text', () => {
|
||||
expect(knuthMorrisPratt('', '')).toBe(-1);
|
||||
expect(knuthMorrisPratt('a', '')).toBe(-1);
|
||||
expect(knuthMorrisPratt('', '')).toBe(0);
|
||||
expect(knuthMorrisPratt('a', '')).toBe(0);
|
||||
expect(knuthMorrisPratt('a', 'a')).toBe(0);
|
||||
expect(knuthMorrisPratt('abcbcglx', 'abca')).toBe(-1);
|
||||
expect(knuthMorrisPratt('abcbcglx', 'bcgl')).toBe(3);
|
||||
|
@ -30,6 +30,10 @@ function buildPatternTable(word) {
|
||||
* @return {number}
|
||||
*/
|
||||
export default function knuthMorrisPratt(text, word) {
|
||||
if (word.length === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let textIndex = 0;
|
||||
let wordIndex = 0;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user