mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 23:21:18 +08:00
Code style fixes.
This commit is contained in:
parent
9e210ae560
commit
5bdcbb397d
@ -72,7 +72,8 @@ a set of rules that precisely define a sequence of operations.
|
|||||||
* **Strings**
|
* **Strings**
|
||||||
* `A` [Levenshtein Distance](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/levenshtein-distance) - minimum edit distance between two sequences
|
* `A` [Levenshtein Distance](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/levenshtein-distance) - minimum edit distance between two sequences
|
||||||
* `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
|
* `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
|
||||||
* `A` [Knuth–Morris–Pratt Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/knuth-morris-pratt) (KMP Algorithm) - substring search
|
* `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)
|
||||||
|
* `A` [Z Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/z-algorithm) - substring search (pattern matching)
|
||||||
* `A` [Rabin Karp Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/rabin-karp) - substring search
|
* `A` [Rabin Karp Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/rabin-karp) - substring search
|
||||||
* `A` [Longest Common Substring](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/longest-common-substring)
|
* `A` [Longest Common Substring](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/longest-common-substring)
|
||||||
* **Searches**
|
* **Searches**
|
||||||
|
@ -1,12 +1,16 @@
|
|||||||
import zAlgorithm from '../zAlgorithm';
|
import zAlgorithm from '../zAlgorithm';
|
||||||
|
|
||||||
describe('zAlgorithm', () => {
|
describe('zAlgorithm', () => {
|
||||||
it('should find word position in given text', () => {
|
it('should find word positions in given text', () => {
|
||||||
expect(zAlgorithm('abcbcglx', 'abca')).toBe(-1);
|
expect(zAlgorithm('abcbcglx', 'abca')).toEqual([]);
|
||||||
expect(zAlgorithm('abcbcglx', 'bcgl')).toBe(3);
|
expect(zAlgorithm('abca', 'abca')).toEqual([0]);
|
||||||
expect(zAlgorithm('abcxabcdabxabcdabcdabcy', 'abcdabcy')).toBe(15);
|
expect(zAlgorithm('abca', 'abcadfd')).toEqual([]);
|
||||||
expect(zAlgorithm('abcxabcdabxabcdabcdabcy', 'abcdabca')).toBe(-1);
|
expect(zAlgorithm('abcbcglabcx', 'abc')).toEqual([0, 7]);
|
||||||
expect(zAlgorithm('abcxabcdabxaabcdabcabcdabcdabcy', 'abcdabca')).toBe(12);
|
expect(zAlgorithm('abcbcglx', 'bcgl')).toEqual([3]);
|
||||||
expect(zAlgorithm('abcxabcdabxaabaabaaaabcdabcdabcy', 'aabaabaaa')).toBe(11);
|
expect(zAlgorithm('abcbcglx', 'cglx')).toEqual([4]);
|
||||||
|
expect(zAlgorithm('abcxabcdabxabcdabcdabcy', 'abcdabcy')).toEqual([15]);
|
||||||
|
expect(zAlgorithm('abcxabcdabxabcdabcdabcy', 'abcdabca')).toEqual([]);
|
||||||
|
expect(zAlgorithm('abcxabcdabxaabcdabcabcdabcdabcy', 'abcdabca')).toEqual([12]);
|
||||||
|
expect(zAlgorithm('abcxabcdabxaabaabaaaabcdabcdabcy', 'aabaabaaa')).toEqual([11]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
|
// The string separator that is being used for "word" and "text" concatenation.
|
||||||
|
const SEPARATOR = '$';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} word
|
* @param {string} zString
|
||||||
* @param {string} text
|
|
||||||
* @return {number[]}
|
* @return {number[]}
|
||||||
*/
|
*/
|
||||||
|
function buildZArray(zString) {
|
||||||
function buildZArray(word, text) {
|
|
||||||
const zString = `${word}$${text}`;
|
|
||||||
const zArray = new Array(zString.length);
|
const zArray = new Array(zString.length);
|
||||||
|
|
||||||
let left = 0;
|
let left = 0;
|
||||||
let right = 0;
|
let right = 0;
|
||||||
let k = 0;
|
let k = 0;
|
||||||
@ -44,14 +45,32 @@ function buildZArray(word, text) {
|
|||||||
/**
|
/**
|
||||||
* @param {string} text
|
* @param {string} text
|
||||||
* @param {string} word
|
* @param {string} word
|
||||||
* @return {number}
|
* @return {number[]}
|
||||||
*/
|
*/
|
||||||
export default function zAlgorithm(text, word) {
|
export default function zAlgorithm(text, word) {
|
||||||
const zArray = buildZArray(word, text);
|
// The list of word's positions in text. Word may be found in the same text
|
||||||
for (let i = 1; i < zArray.length; i += 1) {
|
// in several different positions. Thus it is an array.
|
||||||
if (zArray[i] === word.length) {
|
const wordPositions = [];
|
||||||
return (i - word.length - 1);
|
|
||||||
|
// Concatenate word and string. Word will be a prefix to a string.
|
||||||
|
const zString = `${word}${SEPARATOR}${text}`;
|
||||||
|
|
||||||
|
// Generate Z-array for concatenated string.
|
||||||
|
const zArray = buildZArray(zString);
|
||||||
|
|
||||||
|
// Based on Z-array properties each cell will tell us the length of the match between
|
||||||
|
// the string prefix and current sub-text. Thus we're may find all positions in zArray
|
||||||
|
// with the number that equals to the length of the word (zString prefix) and based on
|
||||||
|
// that positions we'll be able to calculate word positions in text.
|
||||||
|
for (let charIndex = 1; charIndex < zArray.length; charIndex += 1) {
|
||||||
|
if (zArray[charIndex] === word.length) {
|
||||||
|
// Since we did concatenation to form zString we need to subtract prefix
|
||||||
|
// and separator lengths.
|
||||||
|
const wordPosition = charIndex - word.length - SEPARATOR.length;
|
||||||
|
wordPositions.push(wordPosition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
|
||||||
|
// Return the list of word positions.
|
||||||
|
return wordPositions;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user