Knuth–Morris–Pratt algorithm. Searches for the position of
the first occurrence of a specified value in a string.
Parameters:
| Name | Type | Description |
|---|---|---|
str |
String | String. |
substr |
String | Substring. |
- Source:
Returns:
A Number, representing the position
where the specified substring occurs for the first
time, or -1 if it never occurs.
- Type
- Number
Example
var indexOf = require('path-to-algorithm/src/searching/'+
'knuth-morris-pratt').kmp;
console.log(indexOf('hello', 'll')); // 2