Skip to content

Commit 5bdcbb3

Browse files
committedJun 21, 2018
Code style fixes.
1 parent 9e210ae commit 5bdcbb3

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed
 

‎README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ a set of rules that precisely define a sequence of operations.
7272
* **Strings**
7373
* `A` [Levenshtein Distance](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/levenshtein-distance) - minimum edit distance between two sequences
7474
* `B` [Hamming Distance](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/hamming-distance) - number of positions at which the symbols are different
75-
* `A` [Knuth–Morris–Pratt Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/knuth-morris-pratt) (KMP Algorithm) - substring search
75+
* `A` [Knuth–Morris–Pratt Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/knuth-morris-pratt) (KMP Algorithm) - substring search (pattern matching)
76+
* `A` [Z Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/z-algorithm) - substring search (pattern matching)
7677
* `A` [Rabin Karp Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/rabin-karp) - substring search
7778
* `A` [Longest Common Substring](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/longest-common-substring)
7879
* **Searches**
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import zAlgorithm from '../zAlgorithm';
22

33
describe('zAlgorithm', () => {
4-
it('should find word position in given text', () => {
5-
expect(zAlgorithm('abcbcglx', 'abca')).toBe(-1);
6-
expect(zAlgorithm('abcbcglx', 'bcgl')).toBe(3);
7-
expect(zAlgorithm('abcxabcdabxabcdabcdabcy', 'abcdabcy')).toBe(15);
8-
expect(zAlgorithm('abcxabcdabxabcdabcdabcy', 'abcdabca')).toBe(-1);
9-
expect(zAlgorithm('abcxabcdabxaabcdabcabcdabcdabcy', 'abcdabca')).toBe(12);
10-
expect(zAlgorithm('abcxabcdabxaabaabaaaabcdabcdabcy', 'aabaabaaa')).toBe(11);
4+
it('should find word positions in given text', () => {
5+
expect(zAlgorithm('abcbcglx', 'abca')).toEqual([]);
6+
expect(zAlgorithm('abca', 'abca')).toEqual([0]);
7+
expect(zAlgorithm('abca', 'abcadfd')).toEqual([]);
8+
expect(zAlgorithm('abcbcglabcx', 'abc')).toEqual([0, 7]);
9+
expect(zAlgorithm('abcbcglx', 'bcgl')).toEqual([3]);
10+
expect(zAlgorithm('abcbcglx', 'cglx')).toEqual([4]);
11+
expect(zAlgorithm('abcxabcdabxabcdabcdabcy', 'abcdabcy')).toEqual([15]);
12+
expect(zAlgorithm('abcxabcdabxabcdabcdabcy', 'abcdabca')).toEqual([]);
13+
expect(zAlgorithm('abcxabcdabxaabcdabcabcdabcdabcy', 'abcdabca')).toEqual([12]);
14+
expect(zAlgorithm('abcxabcdabxaabaabaaaabcdabcdabcy', 'aabaabaaa')).toEqual([11]);
1115
});
1216
});

‎src/algorithms/string/z-algorithm/zAlgorithm.js

+30-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
// The string separator that is being used for "word" and "text" concatenation.
2+
const SEPARATOR = '$';
3+
14
/**
2-
* @param {string} word
3-
* @param {string} text
5+
* @param {string} zString
46
* @return {number[]}
57
*/
6-
7-
function buildZArray(word, text) {
8-
const zString = `${word}$${text}`;
8+
function buildZArray(zString) {
99
const zArray = new Array(zString.length);
10+
1011
let left = 0;
1112
let right = 0;
1213
let k = 0;
@@ -44,14 +45,32 @@ function buildZArray(word, text) {
4445
/**
4546
* @param {string} text
4647
* @param {string} word
47-
* @return {number}
48+
* @return {number[]}
4849
*/
4950
export default function zAlgorithm(text, word) {
50-
const zArray = buildZArray(word, text);
51-
for (let i = 1; i < zArray.length; i += 1) {
52-
if (zArray[i] === word.length) {
53-
return (i - word.length - 1);
51+
// The list of word's positions in text. Word may be found in the same text
52+
// in several different positions. Thus it is an array.
53+
const wordPositions = [];
54+
55+
// Concatenate word and string. Word will be a prefix to a string.
56+
const zString = `${word}${SEPARATOR}${text}`;
57+
58+
// Generate Z-array for concatenated string.
59+
const zArray = buildZArray(zString);
60+
61+
// Based on Z-array properties each cell will tell us the length of the match between
62+
// the string prefix and current sub-text. Thus we're may find all positions in zArray
63+
// with the number that equals to the length of the word (zString prefix) and based on
64+
// that positions we'll be able to calculate word positions in text.
65+
for (let charIndex = 1; charIndex < zArray.length; charIndex += 1) {
66+
if (zArray[charIndex] === word.length) {
67+
// Since we did concatenation to form zString we need to subtract prefix
68+
// and separator lengths.
69+
const wordPosition = charIndex - word.length - SEPARATOR.length;
70+
wordPositions.push(wordPosition);
5471
}
5572
}
56-
return -1;
73+
74+
// Return the list of word positions.
75+
return wordPositions;
5776
}

0 commit comments

Comments
 (0)
Please sign in to comment.