# Z algorithm (Linear time pattern searching Algorithm)

This algorithm finds all occurrences of a pattern in a text in linear time. Let length of text be n and of pattern be m, then total time taken is O(m + n) with linear space complexity. 

<img src="screenshots/test.gif"  />

The algorithm runs in linear time because we never compare character less than R and with matching we increase R by one so there are at most T comparisons. In mismatch case, mismatch happen only once for each i (because of which R stops), that’s another at most T comparison making overall linear complexity.

## Install

```bash

npm install z-algorithm --save

or 

yarn install z-algorithm

```

# Running the tests

you need to install <a href="https://facebook.github.io/jest/docs/en/22.1/getting-started.html">jest</a> 

```bash

npm test

or 

yarn test

```


# Usage

```js

import z from 'z-algorithm';

let result = z.search('Hello World', 'Hello'); 

```

## Examples

```js

import z from 'z-algorithm';
        
let result = z.search('Hello World', 'Hello'); 
// result = [0]

let result = z.search('Google', 'o'); 
// result = [1,2]

let result = z.search('karimation', 'i'); 
// result = [3,7]   

```


# Author

kerim selmi <a href="http://www.karimation.com">karimation</a>
# License

This project is licensed under the  <a href="LICENSE">MIT License</a>
